diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1 @@ +{} diff --git a/quiz/part1/Part1 Soal1.js b/quiz/part1/Part1 Soal1.js new file mode 100644 index 0000000..6fc5e82 --- /dev/null +++ b/quiz/part1/Part1 Soal1.js @@ -0,0 +1,16 @@ +function angkaPrima(angka) { + if (angka < 2) return false; + + for (let i = 2; i <= Math.sqrt(angka); i++) { + if (angka % i === 0) { + return false; + } + } + return true; +} + +console.log(angkaPrima(3)); +console.log(angkaPrima(7)); +console.log(angkaPrima(6)); +console.log(angkaPrima(23)); +console.log(angkaPrima(33)); diff --git a/quiz/part1/Part1 Soal2.js b/quiz/part1/Part1 Soal2.js new file mode 100644 index 0000000..cc46f5c --- /dev/null +++ b/quiz/part1/Part1 Soal2.js @@ -0,0 +1,14 @@ +function fpb(angka1, angka2) { + while (angka2 !== 0) { + let sisa = angka1 % angka2; + angka1 = angka2; + angka2 = sisa; + } + return angka1; +} + +console.log(fpb(12, 16)); +console.log(fpb(50, 40)); +console.log(fpb(22, 99)); +console.log(fpb(24, 36)); +console.log(fpb(17, 23)); diff --git a/quiz/part1/Part1 Soal3.js b/quiz/part1/Part1 Soal3.js new file mode 100644 index 0000000..6fd0724 --- /dev/null +++ b/quiz/part1/Part1 Soal3.js @@ -0,0 +1,18 @@ +function cariMedian(arr) { + arr.sort((a, b) => a - b); + + let panjang = arr.length; + let tengah = Math.floor(panjang / 2); + + if (panjang % 2 === 1) { + return arr[tengah]; + } else { + return (arr[tengah - 1] + arr[tengah]) / 2; + } +} + +console.log(cariMedian([1, 2, 3, 4, 5])); +console.log(cariMedian([1, 3, 4, 10, 12, 13])); +console.log(cariMedian([3, 4, 7, 6, 10])); +console.log(cariMedian([1, 3, 3])); +console.log(cariMedian([7, 7, 8, 8])); diff --git a/quiz/part1/Part1 Soal4.js b/quiz/part1/Part1 Soal4.js new file mode 100644 index 0000000..15dcdb8 --- /dev/null +++ b/quiz/part1/Part1 Soal4.js @@ -0,0 +1,37 @@ +function cariModus(arr) { + let frekuensi = {}; + let maxCount = 0; + let modus = -1; + + for (let i = 0; i < arr.length; i++) { + let angka = arr[i]; + if (!frekuensi[angka]) { + frekuensi[angka] = 1; + } else { + frekuensi[angka]++; + } + } + + for (let i = 0; i < arr.length; i++) { + let angka = arr[i]; + if (frekuensi[angka] > maxCount) { + maxCount = frekuensi[angka]; + modus = angka; + } + } + + if (maxCount === 1) { + return -1; + } + if (maxCount === arr.length) { + return -1; + } + + return modus; +} + +console.log(cariModus([10, 4, 5, 2, 4])); +console.log(cariModus([5, 10, 10, 6, 5])); +console.log(cariModus([10, 3, 1, 2, 5])); +console.log(cariModus([1, 2, 3, 3, 4, 5])); +console.log(cariModus([7, 7, 7, 7, 7])); diff --git a/quiz/part1/Part1 Soal5.js b/quiz/part1/Part1 Soal5.js new file mode 100644 index 0000000..b075b2f --- /dev/null +++ b/quiz/part1/Part1 Soal5.js @@ -0,0 +1,17 @@ +function ubahHuruf(kata) { + let hasil = ""; + for (let i = 0; i < kata.length; i++) { + let kode = kata.charCodeAt(i); + if (kode === 122) { + hasil += String.fromCharCode(97); + hasil += String.fromCharCode(kode + 1); + } + } + return hasil; +} + +console.log(ubahHuruf("wow")); +console.log(ubahHuruf("developer")); +console.log(ubahHuruf("javascript")); +console.log(ubahHuruf("keren")); +console.log(ubahHuruf("semangat")); diff --git a/quiz/part2/Part2 Soal1.js b/quiz/part2/Part2 Soal1.js new file mode 100644 index 0000000..6d113dc --- /dev/null +++ b/quiz/part2/Part2 Soal1.js @@ -0,0 +1,23 @@ +function digitPerkalianMinimum(angka) { + let minDigit = Infinity; + + for (let i = 1; i <= angka; i++) { + if (angka % i === 0) { + let j = angka / i; + let gabungan = String(i) + String(j); + let panjang = gabungan.length; + + if (panjang < minDigit) { + minDigit = panjang; + } + } + } + + return minDigit; +} + +console.log(digitPerkalianMinimum(24)); +console.log(digitPerkalianMinimum(90)); +console.log(digitPerkalianMinimum(20)); +console.log(digitPerkalianMinimum(179)); +console.log(digitPerkalianMinimum(1)); diff --git a/quiz/part2/Part2 Soal2.js b/quiz/part2/Part2 Soal2.js new file mode 100644 index 0000000..49b41f9 --- /dev/null +++ b/quiz/part2/Part2 Soal2.js @@ -0,0 +1,21 @@ +function urutkanAbjad(str) { + let arr = str.split(""); + + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr.length - 1; j++) { + if (arr[j] > arr[j + 1]) { + let temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + + return arr.join(""); +} + +console.log(urutkanAbjad("hello")); +console.log(urutkanAbjad("truncate")); +console.log(urutkanAbjad("developer")); +console.log(urutkanAbjad("software")); +console.log(urutkanAbjad("aegis")); diff --git a/quiz/part2/Part2 Soal3.js b/quiz/part2/Part2 Soal3.js new file mode 100644 index 0000000..560eb17 --- /dev/null +++ b/quiz/part2/Part2 Soal3.js @@ -0,0 +1,21 @@ +function tukarBesarKecil(kalimat) { + let hasil = ""; + + for (let i = 0; i < kalimat.length; i++) { + let char = kalimat[i]; + + if (char === char.toUpperCase()) { + hasil += char.toLowerCase(); + } else { + hasil += char.toUpperCase(); + } + } + + return hasil; +} + +console.log(tukarBesarKecil("Hello World")); +console.log(tukarBesarKecil("I aM aLAY")); +console.log(tukarBesarKecil("My Name is Bond!!")); +console.log(tukarBesarKecil("IT sHOULD bE me")); +console.log(tukarBesarKecil("001-A-3-5TrdYW")); diff --git a/quiz/part2/Part2 Soal4.js b/quiz/part2/Part2 Soal4.js new file mode 100644 index 0000000..137522f --- /dev/null +++ b/quiz/part2/Part2 Soal4.js @@ -0,0 +1,17 @@ +function checkAB(str) { + for (let i = 0; i < str.length; i++) { + if (str[i] === "a" && str[i + 4] === "b") { + return true; + } + if (str[i] === "b" && str[i + 4] === "a") { + return true; + } + } + return false; +} + +console.log(checkAB("lane borrowed")); +console.log(checkAB("i am sick")); +console.log(checkAB("you are boring")); +console.log(checkAB("barbarian")); +console.log(checkAB("bacon and meat")); diff --git a/quiz/part3/Part3 Soal1.js b/quiz/part3/Part3 Soal1.js new file mode 100644 index 0000000..e9947f7 --- /dev/null +++ b/quiz/part3/Part3 Soal1.js @@ -0,0 +1,27 @@ +function changeMe(arr) { + if (arr.length === 0) { + console.log('""'); + return; + } + + const currentYear = 2023; + + for (let i = 0; i < arr.length; i++) { + let person = { + firstName: arr[i][0], + lastName: arr[i][1], + gender: arr[i][2], + age: arr[i][3] ? currentYear - arr[i][3] : "Invalid Birth Year", + }; + + console.log(`${i + 1}. ${person.firstName} ${person.lastName}:`); + console.log(person); + } +} + +changeMe([ + ["Christ", "Evans", "Male", 1982], + ["Robert", "Downey", "Male"], +]); + +changeMe([]); diff --git a/quiz/part3/Part3 Soal2.js b/quiz/part3/Part3 Soal2.js new file mode 100644 index 0000000..d7eb052 --- /dev/null +++ b/quiz/part3/Part3 Soal2.js @@ -0,0 +1,45 @@ +function shoppingTime(memberId, money) { + if (!memberId) { + return "Mohon maaf, toko X hanya berlaku untuk member saja"; + } + if (money < 50000) { + return "Mohon maaf, uang tidak cukup"; + } + + let saleItems = [ + ["Sepatu Stacattu", 1500000], + ["Baju Zoro", 500000], + ["Baju H&N", 250000], + ["Sweater Uniklooh", 175000], + ["Casing Handphone", 50000], + ]; + + let listPurchased = []; + let sisaUang = money; + + for (let i = 0; i < saleItems.length; i++) { + let item = saleItems[i][0]; + let harga = saleItems[i][1]; + if (sisaUang >= harga) { + listPurchased.push(item); + sisaUang -= harga; + } + } + + return { + memberId: memberId, + money: money, + listPurchased: listPurchased, + changeMoney: sisaUang, + }; +} + +console.log(shoppingTime("1820RzKrnWn08", 2475000)); + +console.log(shoppingTime("82Ku8Ma742", 170000)); + +console.log(shoppingTime("", 2475000)); + +console.log(shoppingTime("234JdhweRxa53", 15000)); + +console.log(shoppingTime()); diff --git a/quiz/part3/Part3 Soal3.js b/quiz/part3/Part3 Soal3.js new file mode 100644 index 0000000..b0bd006 --- /dev/null +++ b/quiz/part3/Part3 Soal3.js @@ -0,0 +1,64 @@ +function countProfit(shoppers) { + var listBarang = [ + ["Sepatu Stacattu", 1500000, 10], + ["Baju Zoro", 500000, 2], + ["Sweater Uniklooh", 175000, 1], + ]; + + if (shoppers.length === 0) { + return []; + } + + let result = []; + + for (let i = 0; i < listBarang.length; i++) { + let productName = listBarang[i][0]; + let price = listBarang[i][1]; + let stock = listBarang[i][2]; + + let obj = { + product: productName, + shoppers: [], + leftOver: stock, + totalProfit: 0, + }; + + for (let j = 0; j < shoppers.length; j++) { + let buyer = shoppers[j]; + + if (buyer.product === productName && buyer.amount <= obj.leftOver) { + obj.shoppers.push(buyer.name); + obj.leftOver -= buyer.amount; + obj.totalProfit += buyer.amount * price; + } + } + + result.push(obj); + } + + return result; +} + +console.log( + countProfit([ + { name: "Windi", product: "Sepatu Stacattu", amount: 2 }, + { name: "Vanessa", product: "Sepatu Stacattu", amount: 3 }, + { name: "Rani", product: "Sweater Uniklooh", amount: 2 }, + ]) +); + +console.log( + countProfit([ + { name: "Windi", product: "Sepatu Stacattu", amount: 8 }, + { name: "Vanessa", product: "Sepatu Stacattu", amount: 10 }, + { name: "Rani", product: "Sweater Uniklooh", amount: 1 }, + { name: "Devi", product: "Baju Zoro", amount: 1 }, + { name: "Lisa", product: "Baju Zoro", amount: 1 }, + ]) +); + +console.log( + countProfit([{ name: "Windi", product: "Sepatu Naiki", amount: 5 }]) +); + +console.log(countProfit([])); diff --git a/quiz/ujian/Ujian Soal 3.js b/quiz/ujian/Ujian Soal 3.js new file mode 100644 index 0000000..50eba2d --- /dev/null +++ b/quiz/ujian/Ujian Soal 3.js @@ -0,0 +1,45 @@ +function highestScore(students) { + let result = {}; + + for (let i = 0; i < students.length; i++) { + let student = students[i]; + let kelas = student.class; + + if (!result[kelas]) { + result[kelas] = { + name: student.name, + score: student.score, + }; + } else { + if (student.score > result[kelas].score) { + result[kelas] = { + name: student.name, + score: student.score, + }; + } + } + } + + return result; +} + +console.log( + highestScore([ + { name: "Dimitri", score: 90, class: "foxes" }, + { name: "Alexei", score: 85, class: "wolves" }, + { name: "Sergei", score: 74, class: "foxes" }, + { name: "Anastasia", score: 78, class: "wolves" }, + ]) +); + +console.log( + highestScore([ + { name: "Alexander", score: 100, class: "foxes" }, + { name: "Alisa", score: 76, class: "wolves" }, + { name: "Vladimir", score: 92, class: "foxes" }, + { name: "Albert", score: 71, class: "wolves" }, + { name: "Viktor", score: 80, class: "tigers" }, + ]) +); + +console.log(highestScore([])); diff --git a/quiz/ujian/Ujian Soal2.js b/quiz/ujian/Ujian Soal2.js new file mode 100644 index 0000000..2eb3070 --- /dev/null +++ b/quiz/ujian/Ujian Soal2.js @@ -0,0 +1,35 @@ +function naikAngkot(arrPenumpang) { + let rute = ["A", "B", "C", "D", "E", "F"]; + let hasil = []; + + for (let i = 0; i < arrPenumpang.length; i++) { + let penumpang = arrPenumpang[i][0]; + let naikDari = arrPenumpang[i][1]; + let tujuan = arrPenumpang[i][2]; + + let startIndex = rute.indexOf(naikDari); + let endIndex = rute.indexOf(tujuan); + + let bayar = (endIndex - startIndex) * 2000; + + let obj = { + penumpang: penumpang, + naikDari: naikDari, + tujuan: tujuan, + bayar: bayar, + }; + + hasil.push(obj); + } + + return hasil; +} + +console.log( + naikAngkot([ + ["Dimitri", "B", "F"], + ["Icha", "A", "B"], + ]) +); + +console.log(naikAngkot([])); diff --git a/quiz/ujian/Ujian Soal4.js b/quiz/ujian/Ujian Soal4.js new file mode 100644 index 0000000..d863a92 --- /dev/null +++ b/quiz/ujian/Ujian Soal4.js @@ -0,0 +1,40 @@ +function graduates(students) { + let result = {}; + + for (let i = 0; i < students.length; i++) { + let student = students[i]; + let kelas = student.class; + + result[kelas] = []; + } + + if (student.score > 75) { + result[kelas].push({ + name: student.name, + score: student.score, + }); + } +} + +return result; + +console.log( + graduates([ + { name: "Dimitri", score: 90, class: "foxes" }, + { name: "Alexei", score: 85, class: "wolves" }, + { name: "Sergei", score: 74, class: "foxes" }, + { name: "Anastasia", score: 78, class: "wolves" }, + ]) +); + +console.log( + graduates([ + { name: "Alexander", score: 100, class: "foxes" }, + { name: "Alisa", score: 76, class: "wolves" }, + { name: "Vladimir", score: 92, class: "foxes" }, + { name: "Albert", score: 71, class: "wolves" }, + { name: "Viktor", score: 80, class: "tigers" }, + ]) +); + +console.log(graduates([])); diff --git a/quiz/ujian/Ujian1 Soal1.js b/quiz/ujian/Ujian1 Soal1.js new file mode 100644 index 0000000..6b13a59 --- /dev/null +++ b/quiz/ujian/Ujian1 Soal1.js @@ -0,0 +1,48 @@ +function deepSum(arr) { + if (arr.length === 0) { + return "No number"; + } + + let total = 0; + + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr[i].length; j++) { + for (let k = 0; k < arr[i][j].length; k++) { + total += arr[i][j][k]; + } + } + } + + return total; +} + +console.log( + deepSum([ + [ + [4, 5, 6], + [9, 1, 2, 10], + [9, 4, 3], + ], + [ + [4, 14, 31], + [9, 10, 18, 12, 20], + [1, 4, 90], + ], + [ + [2, 5, 10], + [3, 4, 5], + [2, 4, 5, 10], + ], + ]) +); + +console.log( + deepSum([ + [[20, 10], [15], [1, 1]], + [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [2], [9, 11]], + [[3, 5, 1], [1, 5, 3], [1]], + [[2]], + ]) +); + +console.log(deepSum([]));