diff --git a/quiz/part1/soal1.js b/quiz/part1/soal1.js new file mode 100644 index 0000000..c95e096 --- /dev/null +++ b/quiz/part1/soal1.js @@ -0,0 +1,18 @@ +//cek apakah angka yang dikirim adalah angka prima atau bukan? +//cek google bagi yang ga tau apa itu angka prima +function angkaPrima(angka) { + // you can only write your code here! + for (let i = 2; i <= Math.sqrt(angka); i++) { + if (angka % i === 0) { + return false; // Jika ada pembagi selain 1 dan dirinya sendiri, maka bukan angka prima + } + } + return true; +} + + // TEST CASES + console.log(angkaPrima(3)); // true + console.log(angkaPrima(7)); // true + console.log(angkaPrima(6)); // false + console.log(angkaPrima(23)); // true + console.log(angkaPrima(33)); // false \ No newline at end of file diff --git a/quiz/part1/soal2.js b/quiz/part1/soal2.js new file mode 100644 index 0000000..953fc7d --- /dev/null +++ b/quiz/part1/soal2.js @@ -0,0 +1,29 @@ +//cari faktor persekutuan terbesar +function fpb(angka1, angka2) { + // you can only write your code here! + let result = []; + let smaller, bigger; + if (angka1 <= angka2) { + smaller = angka1; + } else { + smaller = angka2; + } + if (angka1 >= angka2) { + bigger = angka1; + } else { + bigger = angka2; + } + for (let i = 1; i < smaller; i++) { + if (smaller % i == 0 && bigger % i == 0) { + result.push(i); + } + } + return Math.max(...result); +} + + // TEST CASES + console.log(fpb(12, 16)); // 4 + console.log(fpb(50, 40)); // 10 + console.log(fpb(22, 99)); // 11 + console.log(fpb(24, 36)); // 12 + console.log(fpb(17, 23)); // 1 \ No newline at end of file diff --git a/quiz/part1/soal3.js b/quiz/part1/soal3.js new file mode 100644 index 0000000..baec106 --- /dev/null +++ b/quiz/part1/soal3.js @@ -0,0 +1,26 @@ +function cariMedian(arr) { + // you can only write your code here! + // Urutkan array terlebih dahulu + arr.sort((a, b) => a - b); + + const len = arr.length; + + // Jika panjang array ganjil, kembalikan nilai tengah + if (len % 2 !== 0) { + return arr[Math.floor(len / 2)]; + } + + // Jika panjang array genap, kembalikan rata-rata dari dua angka tengah + else { + const mid1 = arr[len / 2 - 1]; + const mid2 = arr[len / 2]; + return (mid1 + mid2) / 2; + } +} + + // TEST CASES + console.log(cariMedian([1, 2, 3, 4, 5])); // 3 + console.log(cariMedian([1, 3, 4, 10, 12, 13])); // 7 + console.log(cariMedian([3, 4, 7, 6, 10])); // 6 + console.log(cariMedian([1, 3, 3])); // 3 + console.log(cariMedian([7, 7, 8, 8])); // 7.5 \ No newline at end of file diff --git a/quiz/part1/soal4.js b/quiz/part1/soal4.js new file mode 100644 index 0000000..2b02f64 --- /dev/null +++ b/quiz/part1/soal4.js @@ -0,0 +1,53 @@ +/* +Diberikan sebuah function cariModus(arr) yang menerima sebuah array angka. Function akan me-return modus dari array atau deret angka tersebut. Modus adalah angka dari sebuah deret yang paling banyak atau paling sering muncul. Contoh, modus dari [10, 4, 5, 2, 4] adalah 4. Jika modus tidak ditemukan, function akan me-return -1. Apabila ditemukan lebih dari dua nilai modus, tampilkan nilai modus yang paling pertama muncul (dihitung dari kiri ke kanan). Dan apabila dialam modus hanya ada 1 nilai yang sama maka function akan me-return -1, Contohnya [1, 1, 1] adalah -1. +*/ +function cariModus(arr) { + // you can only write your code here! + arr.sort((a, b) => a - b); + + let maxCount = 0; + let currentCount = 1; + let modus = -1; + let modeCount = 0; + + // Lakukan pencarian linear setelah array diurutkan + for (let i = 1; i < arr.length; i++) { + if (arr[i] === arr[i - 1]) { + // Jika angka sama, tambah count + currentCount++; + } else { + // Jika angka berbeda, reset count dan periksa apakah lebih besar dari maxCount + if (currentCount > maxCount) { + maxCount = currentCount; + modus = arr[i - 1]; + modeCount = 1; + } else if (currentCount === maxCount) { + modeCount++; + } + currentCount = 1; + } + } + + // Periksa jika angka terakhir juga merupakan modus + if (currentCount > maxCount) { + maxCount = currentCount; + modus = arr[arr.length - 1]; + modeCount = 1; + } else if (currentCount === maxCount) { + modeCount++; + } + + // Jika ada hanya satu modus (semua angka sama), return -1 + if (maxCount === arr.length || modeCount > 1) { + return -1; + } + + return modus; +} + + // TEST CASES + console.log(cariModus([10, 4, 5, 2, 4])); // 4 + console.log(cariModus([5, 10, 10, 6, 5])); // 5 + console.log(cariModus([10, 3, 1, 2, 5])); // -1 + console.log(cariModus([1, 2, 3, 3, 4, 5])); // 3 + console.log(cariModus([7, 7, 7, 7, 7])); // -1 \ No newline at end of file diff --git a/quiz/part1/soal5.js b/quiz/part1/soal5.js new file mode 100644 index 0000000..0cc9fb3 --- /dev/null +++ b/quiz/part1/soal5.js @@ -0,0 +1,27 @@ +//sistem ubah hurufnya misal huruf a diubah menjadi b, c menjadi d, b menjadi c, z menjadi a +//intinya ubah huruf menjadi huruf setelahnya +function ubahHuruf(kata) { + // you can only write your code here! + let hasil = ''; + + for (let i = 0; i < kata.length; i++) { + let char = kata[i]; + + // Cek jika karakter adalah 'z' + if (char === 'z') { + hasil += 'a'; + } else { + // Ubah huruf ke huruf berikutnya + hasil += String.fromCharCode(char.charCodeAt(0) + 1); + } + } + + return hasil; + } + + // TEST CASES + console.log(ubahHuruf('wow')); // xpx + console.log(ubahHuruf('developer')); // efwfmpqfs + console.log(ubahHuruf('javascript')); // kbwbtdsjqu + console.log(ubahHuruf('keren')); // lfsfo + console.log(ubahHuruf('semangat')); // tfnbohbu \ No newline at end of file diff --git a/quiz/part2/soal1.js b/quiz/part2/soal1.js new file mode 100644 index 0000000..e1f002e --- /dev/null +++ b/quiz/part2/soal1.js @@ -0,0 +1,43 @@ +/* +Diberikan sebuah function digitPerkalianMinimum(angka) yang menerima satu parameter angka. Function akan mengembalikan jumlah digit minimal dari angka yang merupakan faktor angka parameter tersebut, Contoh, jika angka adalah 24, maka faktornya adalah 1 * 24, 2 * 12, 3 * 8, dan 4 * 6. Dari antara faktor tersebut, yang digit nya paling sedikit adalah 3 * 8 atau 4 * 6, sehingga function akan me-return 2. + +1 * 24 = 3 digit (124) +3 * 8 = 2 digit (38) +4 * 6 = 2 digit (46) + +karena dibawah ini 2 digit dan terkecil +3 * 8 = 2 digit (38) +4 * 6 = 2 digit (46) + +ya jadikan dia minimum + +misal faktronya angka 1 hanyalah +1*1 = 2 digit (11) + +Return 2 +*/ + +function digitPerkalianMinimum(angka){ + let minDigits = Infinity; // Menginisialisasi dengan angka terbesar yang mungkin + + // Mencari semua pasangan faktor + for (let i = 1; i <= Math.sqrt(angka); i++) { + if (angka % i === 0) { + let faktor1 = i; + let faktor2 = angka / i; + + // Menghitung jumlah digit dari hasil perkalian faktor1 dan faktor2 + let hasilPerkalian = faktor1 * faktor2; + let digitCount = (hasilPerkalian + '').length; // Menghitung jumlah digit + + // Menyimpan jumlah digit minimum + if (digitCount < minDigits) { + minDigits = digitCount; + } + } + } + return minDigits; +} + +console.log(digitPerkalianMinimum(24)); +console.log(digitPerkalianMinimum(60)); \ No newline at end of file diff --git a/quiz/part2/soal2.js b/quiz/part2/soal2.js new file mode 100644 index 0000000..5108c02 --- /dev/null +++ b/quiz/part2/soal2.js @@ -0,0 +1,29 @@ +//DILARANG MENGGUNAKAN METHOD SORT, PELAJARI ALGORITMA SORTING YANG ADA DI GOOGLE +//saran sih pake bubblesort walau tidak efisien tapi bagus buat belajar sorting +function urutkanAbjad(str) { + // you can only write your code here! + let arr = str.split(''); + + // Bubble Sort: membandingkan setiap elemen dan menukarnya jika perlu + for (let i = 0; i < arr.length - 1; i++) { + for (let j = 0; j < arr.length - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + // Tukar elemen jika urutannya salah + let temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + + // Menggabungkan kembali array yang sudah diurutkan menjadi string + return arr.join(''); +} + + + // TEST CASES + console.log(urutkanAbjad('hello')); // 'ehllo' + console.log(urutkanAbjad('truncate')); // 'acenrttu' + console.log(urutkanAbjad('developer')); // 'deeeloprv' + console.log(urutkanAbjad('software')); // 'aeforstw' + console.log(urutkanAbjad('aegis')); // 'aegis' \ No newline at end of file diff --git a/quiz/part2/soal3.js b/quiz/part2/soal3.js new file mode 100644 index 0000000..58ecf85 --- /dev/null +++ b/quiz/part2/soal3.js @@ -0,0 +1,19 @@ +//TIPS: gunakan method toUpperCase() dan toLowerCase() +function tukarBesarKecil(kalimat) { + // you can only write your code here!\ + let hasil = ""; + for (i = 0; i < kalimat.length; i++){ + if (kalimat[i] === kalimat[i].toUpperCase()){ + hasil += kalimat[i].toLowerCase(); + } else { + hasil += kalimat[i].toUpperCase(); + } + }; + return hasil; +}; + // TEST CASES + console.log(tukarBesarKecil('Hello World')); // "hELLO wORLD" + console.log(tukarBesarKecil('I aM aLAY')); // "i Am Alay" + console.log(tukarBesarKecil('My Name is Bond!!')); // "mY nAME IS bOND!!" + console.log(tukarBesarKecil('IT sHOULD bE me')); // "it Should Be ME" + console.log(tukarBesarKecil('001-A-3-5TrdYW')); // "001-a-3-5tRDyw" \ No newline at end of file diff --git a/quiz/part2/soal4.js b/quiz/part2/soal4.js new file mode 100644 index 0000000..774779e --- /dev/null +++ b/quiz/part2/soal4.js @@ -0,0 +1,41 @@ +/* +Diberikan sebuah function checkAB(str) yang menerima satu parameter berupa string. function tersebut mengembalikan nilai true jika di dalam string tersebut terdapat karakter a dan b yang memiliki jarak 3 karakter lain minimal satu kali. Jika tidak ditemukan sama sekali, kembalikan nilai false. Jarak bisa dari a ke b, atau b ke a. + +contoh: +barbarian kenapa bisa true? +karena di bagian 'barian' terdapat b dipisah 3 karakter ari(totalnya 3) dan ketemu a + +Spasi juga dianggap sebagai karakter +*/ + +function checkAB(str) { + // you can only write your code here! + let posisiA = []; + let posisiB = []; + + // Loop untuk mencari posisi huruf 'a' dan 'b' + for (let i = 0; i < str.length; i++) { + if (str[i] === 'a') { + posisiA.push(i); + } else if (str[i] === 'b') { + posisiB.push(i); + } + } + + // Memeriksa jarak antara 'a' dan 'b' + for (let i = 0; i < posisiA.length; i++) { + for (let j = 0; j < posisiB.length; j++) { + if (Math.abs(posisiA[i] - posisiB[j]) === 4) { + return true; // Jika jarak antara 'a' dan 'b' adalah 3 (perbedaan posisi 4) + } + } + } + return false; // Jika tidak ada jarak yang memenuhi kriteria +} + + // TEST CASES + console.log(checkAB('lane borrowed')); // true + console.log(checkAB('i am sick')); // false + console.log(checkAB('you are boring')); // true + console.log(checkAB('barbarian')); // true + console.log(checkAB('bacon and meat')); // false \ No newline at end of file diff --git a/quiz/part3/soal1.js b/quiz/part3/soal1.js new file mode 100644 index 0000000..16b6704 --- /dev/null +++ b/quiz/part3/soal1.js @@ -0,0 +1,36 @@ +function changeMe(arr) { + // you can only write your code here! + for (let i = 0; i < arr.length; i++) { + let aktor = arr[i]; + let firstName = aktor[0]; + let secondName = aktor[1]; + let gender = aktor[2]; + let birthYear = aktor[3]; + + if (typeof birthYear !== "number"){ + birthYear = 'Invalid Birth Year'; + } + + + console.log(`${i + 1}. ${firstName} ${secondName} + {first name: ${firstName}, + second name: ${secondName}, + gender: ${gender}, + lahir: ${birthYear}}` + ) + } +} + // TEST CASES + changeMe([['Christ', 'Evans', 'Male', 1982], ['Robert', 'Downey', 'Male']]); // 1. Christ Evans: + // Christ Evans: { firstName: 'Christ', + // lastName: 'Evans', + // gender: 'Male', + // age: 41 } 2023 - 1982 = 41 kan yak wkwk + // Robert Downey: { firstName: 'Robert', + // lastName: 'Downey', + // gender: 'Male', + // age: 'Invalid Birth Year' } + + //intinya bagaimana cara kalian bisa menampilkan output diatas, sebebas dan sekreatif kalian. + + changeMe([]); // "" \ No newline at end of file diff --git a/quiz/part3/soal2.js b/quiz/part3/soal2.js new file mode 100644 index 0000000..0990b00 --- /dev/null +++ b/quiz/part3/soal2.js @@ -0,0 +1,79 @@ +/* +Diberikan sebuah function shoppingTime(memberId, money) yang menerima dua parameter berupa string dan number. Parameter pertama merupakan memberId dan parameter ke-2 merupakan value uang yang dibawa oleh member tersebut. + +Toko X sedang melakukan SALE untuk beberapa barang, yaitu: + +Sepatu brand Stacattu seharga 1500000 +Baju brand Zoro seharga 500000 +Baju brand H&N seharga 250000 +Sweater brand Uniklooh seharga 175000 +Casing Handphone seharga 50000 +Buatlah function yang akan mengembalikan sebuah object dimana object tersebut berisikan informasi memberId, money, listPurchased dan changeMoney. + +Jika memberId kosong maka tampilkan "Mohon maaf, toko X hanya berlaku untuk member saja" +Jika uang yang dimiliki kurang dari 50000 maka tampilkan "Mohon maaf, uang tidak cukup" +Member yang berbelanja di toko X akan membeli barang yang paling mahal terlebih dahulu dan akan membeli barang-barang yang sedang SALE masing-masing 1 jika uang yang dimilikinya masih cukup. +Contoh jika inputan memberId: '324193hDew2' dan money: 700000 + +maka output: + +{ memberId: '324193hDew2', money: 700000, listPurchased: [ 'Baju Zoro', 'Sweater Uniklooh' ], changeMoney: 25000 } +*/ + +function shoppingTime(memberId, money) { + // you can only write your code here! + const listBarang = [ + ["Sepatu Stacattu", 1500000], + ["Baju Zoro", 500000], + ["Baju H&N", 250000], + ["Sweater Uniklooh", 175000], + ["Casing Handphone", 50000] + ]; + + if (!memberId || memberId === ''){ + return 'Mohon maaf, toko X hanya berlaku untuk member saja'; + } if (money <= 50000){ + return 'Mohon maaf, uang tidak cukup'; + } + + let listBelanja = []; + let sisaUang = money; + + for (let i = 0; i < listBarang.length; i++) { + const namaBarang = listBarang[i][0]; + const hargaBarang = listBarang[i][1]; + + if (sisaUang >= hargaBarang) { + listBelanja.push(namaBarang); + sisaUang -= hargaBarang; + } + } + + return { + memberId: memberId, + money: money, + listPurchased: listBelanja, + changeMoney: sisaUang + } + }; + + // TEST CASES + console.log(shoppingTime('1820RzKrnWn08', 2475000)); + //{ memberId: '1820RzKrnWn08', + // money: 2475000, + // listPurchased: + // [ 'Sepatu Stacattu', + // 'Baju Zoro', + // 'Baju H&N', + // 'Sweater Uniklooh', + // 'Casing Handphone' ], + // changeMoney: 0 } + console.log(shoppingTime('82Ku8Ma742', 170000)); + //{ memberId: '82Ku8Ma742', + // money: 170000, + // listPurchased: + // [ 'Casing Handphone' ], + // changeMoney: 120000 } + console.log(shoppingTime('', 2475000)); //Mohon maaf, toko X hanya berlaku untuk member saja + console.log(shoppingTime('234JdhweRxa53', 15000)); //Mohon maaf, uang tidak cukup + console.log(shoppingTime()); ////Mohon maaf, toko X hanya berlaku untuk member saja \ No newline at end of file diff --git a/quiz/part3/soal3.js b/quiz/part3/soal3.js new file mode 100644 index 0000000..5df9ff3 --- /dev/null +++ b/quiz/part3/soal3.js @@ -0,0 +1,91 @@ +/* +Toko X yang sedang melakukan SALE ingin menghitung jumlah profit untuk setiap jenis barang yang terjual pada hari itu. + +Barang-barang SALE yang akan dihitung penjualannya: + +Sepatu brand Stacattu seharga 1500000 dan stock barang yang tesedia 10 +Baju brand Zoro seharga 500000 dan stock barang yang tesedia 2 +Sweater brand Uniklooh seharga 175000 dan stock barang yang tersedia 1 +Function akan menerima array yang berisikan object pembeli (nama pembeli, barang yang ingin dibeli dan jumlah barang yang dibelinya). Jika stock barang kurang dari jumlah yang ingin dibeli oleh pembeli maka pembeli batal untuk membeli barang tersebut. + +Function countProfit akan mengembalikan/me-return sebuah array of object dimana array tersebut berisi objek-objek barang dari toko X tersebut yang berisikan info nama barang, siapa saja yang membeli, sisa stock barang dan total pemasukan untuk barang tersebut +*/ + +function countProfit(shoppers) { + var listBarang = [ + ['Sepatu Stacattu', 1500000, 10], + ['Baju Zoro', 500000, 2], + ['Sweater Uniklooh', 175000, 1] + ]; + + const result = []; + + for (let i = 0; i < listBarang.length; i++) { + const product = listBarang[i]; + const pembeli = []; + let sisaStock = product[2]; + let totalProfit = 0; + + for (let j = 0; j < shoppers.length; j++) { + const shopper = shoppers[j]; + + if (shopper.product === product[0] && shopper.amount <= sisaStock) { + pembeli.push(shopper.name); + sisaStock -= shopper.amount; + totalProfit += shopper.amount * product[1]; + } + } + + result.push({ + product: product[0], + shoppers: pembeli, + leftOver: sisaStock, + totalProfit: totalProfit + }); + } + return result; + // you can only write your code here! +} + + // TEST CASES + console.log(countProfit([{name: 'Windi', product: 'Sepatu Stacattu', amount: 2}, {name: 'Vanessa', product: 'Sepatu Stacattu', amount: 3}, {name: 'Rani', product: 'Sweater Uniklooh', amount: 2}])); + //[ { product: 'Sepatu Stacattu', + // shoppers: [ 'Windi', 'Vanessa' ], + // leftOver: 5, + // totalProfit: 7500000 }, + // { product: 'Baju Zoro', + // shoppers: [], + // leftOver: 2, + // totalProfit: 0 }, + // { product: 'Sweater Uniklooh', + // shoppers: [], + // leftOver: 1, + // totalProfit: 0 } ] + + 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}])); + // [ { product: 'Sepatu Stacattu', + // shoppers: [ 'Windi' ], + // leftOver: 2, + // totalProfit: 12000000 }, + // { product: 'Baju Zoro', + // shoppers: [ 'Devi', 'Lisa' ], + // leftOver: 0, + // totalProfit: 1000000 }, + // { product: 'Sweater Uniklooh', + // shoppers: [ 'Rani' ], + // leftOver: 0, + // totalProfit: 175000 } ] + console.log(countProfit([{name: 'Windi', product: 'Sepatu Naiki', amount: 5}])); + // [ { product: 'Sepatu Stacattu', + // shoppers: [], + // leftOver: 10, + // totalProfit: 0 }, + // { product: 'Baju Zoro', + // shoppers: [], + // leftOver: 2, + // totalProfit: 0 }, + // { product: 'Sweater Uniklooh', + // shoppers: [], + // leftOver: 1, + // totalProfit: 0 } ] + console.log(countProfit([])); //[] \ No newline at end of file diff --git a/quiz/ujian/soal1.js b/quiz/ujian/soal1.js new file mode 100644 index 0000000..26924ed --- /dev/null +++ b/quiz/ujian/soal1.js @@ -0,0 +1,64 @@ +/* +Implementasikan function deepSum untuk mendapatkan jumlah pertambahan dari angka-angka yang terdapat di dalam array +*/ + +function deepSum (arr) { + // Code disini + + let total = 0; + let foundNumber = false; + + for (let i = 0; i < arr.length; i++){ + let matrix = arr[i]; + for (let j = 0; j < matrix.length; j++){ + let row = matrix[j]; + for (let k = 0; k < row.length; k++){ + total += row[k]; + foundNumber = true; + }; + }; + }; + return foundNumber ? total : "No number"; + }; + + //TEST CASE + 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] + ] + ])); // 316 + + 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] + ] + ])); // 156 + + console.log(deepSum([])); // No number \ No newline at end of file diff --git a/quiz/ujian/soal2.js b/quiz/ujian/soal2.js new file mode 100644 index 0000000..72f4c73 --- /dev/null +++ b/quiz/ujian/soal2.js @@ -0,0 +1,40 @@ +/* +Diberikan function naikAngkot(listPenumpang) yang akan menerima satu parameter berupa array dua dimensi. Function akan me-return array of object. + +Diberikan sebuah rute, dari A - F. Penumpang diwajibkan membayar Rp2000 setiap melewati satu rute. + +Contoh: input: [['Dimitri', 'B', 'F']] output: [{ penumpang: 'Dimitri', naikDari: 'B', tujuan: 'F', bayar: 8000 }] +*/ + +function naikAngkot(arrPenumpang) { + let rute = ['A', 'B', 'C', 'D', 'E', 'F']; + //your code here + let result = []; + + for (let i = 0; i < arrPenumpang.length; i++){ + let namaPenumpang = arrPenumpang[i][0]; + let lokasiAwal = arrPenumpang[i][1]; + let tujuanPenumpang = arrPenumpang[i][2]; + + let indexAwal = rute.indexOf(lokasiAwal); + let indexTujuan = rute.indexOf(tujuanPenumpang); + + let jarak = Math.abs(indexTujuan - indexAwal); + let ongkos = jarak * 2000; + + result.push({ + penumpang: namaPenumpang, + naikDari: lokasiAwal, + tujuan: tujuanPenumpang, + bayar: ongkos + }); + }; + return result; + }; + + //TEST CASE + console.log(naikAngkot([['Dimitri', 'B', 'F'], ['Icha', 'A', 'B']])); + // [ { penumpang: 'Dimitri', naikDari: 'B', tujuan: 'F', bayar: 8000 }, + // { penumpang: 'Icha', naikDari: 'A', tujuan: 'B', bayar: 2000 } ] + + console.log(naikAngkot([])); //[] \ No newline at end of file diff --git a/quiz/ujian/soal3.js b/quiz/ujian/soal3.js new file mode 100644 index 0000000..0b6ecdc --- /dev/null +++ b/quiz/ujian/soal3.js @@ -0,0 +1,84 @@ +function highestScore (students) { + // Code disini + let result = {}; + + for (let i = 0; i < students.length; i++){ + const student = students[i]; + const kelas = student.class; + + if (!result[kelas] || student.score > result[kelas].score){ + result[kelas] = { + nama: student.name, + nilai: student.score + }; + }; + }; + return result; + }; + + // TEST CASE + 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' + } + ])); + + // { + // foxes: { name: 'Dimitri', score: 90 }, + // wolves: { name: 'Alexei', score: 85 } + // } + + + 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' + } + ])); + + // { + // foxes: { name: 'Alexander', score: 100 }, + // wolves: { name: 'Alisa', score: 76 }, + // tigers: { name: 'Viktor', score: 80 } + // } + + + console.log(highestScore([])); //{} \ No newline at end of file diff --git a/quiz/ujian/soal4.js b/quiz/ujian/soal4.js new file mode 100644 index 0000000..f904d3a --- /dev/null +++ b/quiz/ujian/soal4.js @@ -0,0 +1,119 @@ +/* +Implementasikan function graduates untuk mendapatkan daftar student yang lulus dengan aturan: + +Student dapat dinyatakan lulus apabila score lebih besar dari 75. +Masukkan name dan score dari student ke class yang dia ikuti. +Student yang tidak lulus tidak perlu ditampilkan. +Output yang diharapkan berupa Object dengan format sebagai berikut: + +{ + : [ + { name: , score: }, + ... + ], + : [ + { name: , score: }, + ... + ], + : [] //NOTE: Jika tidak ada student yang lulus, class ini akan diisi oleh array kosong +} +*/ + +function graduates (students) { + // Code disini + + let lulus = {}; + + for (let i = 0; i < students.length; i++){ + const student = students[i]; + const kelas = student.class; + + if (student.score >= 75){ + if (!lulus[kelas]){ + lulus[kelas] = []; + } + lulus[kelas].push({ + name: student.name, + score: student.score + }); + }; + }; + return lulus; + } + + 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' + } + ])); + + // { + // foxes: [ + // { name: 'Dimitri', score: 90 } + // ], + // wolves: [ + // { name: 'Alexei' , score: 85 }, + // { name: 'Anastasia', score: 78 } + // ] + // } + + 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' + } + ])); + + // { + // foxes: [ + // { name: 'Alexander', score: 100 }, + // { name: 'Vladimir', score: 92 } + // ], + // wolves: [ + // { name: 'Alisa', score: 76 }, + // ], + // tigers: [ + // { name: 'Viktor', score: 80 } + // ] + // } + + + console.log(graduates([])); //{} \ No newline at end of file