diff --git a/quiz/part1/README.md b/quiz/part1/README.md deleted file mode 100644 index 526078e..0000000 --- a/quiz/part1/README.md +++ /dev/null @@ -1,78 +0,0 @@ -# Logic Challenge tanpa tipe data object | Part 1 - -Sabar jangan langsung ke object dulu, kalian harus asah logic lagi wkwk - -## Soal 1 -```js -//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! -} - -// 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 -``` -## Soal 2 -```js -//cari faktor persekutuan terbesar -function fpb(angka1, angka2) { - // you can only write your code here! -} - -// 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 -``` -## Soal 3 -```js -function cariMedian(arr) { - // you can only write your code here! -} - -// 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 -``` - -## Soal 4 -```js -/* -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! -} - -// 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 -``` -## Soal 5 -```js -//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! -} - -// 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/part1/soal1.js b/quiz/part1/soal1.js new file mode 100644 index 0000000..f87530b --- /dev/null +++ b/quiz/part1/soal1.js @@ -0,0 +1,17 @@ +function angkaPrima(angka) { + let penentu = 0 + if (angka <= 1 ) {return false} + for (let i = 0; i <= angka; i++) { + if (angka%i===0) { + penentu++ + } + } + return penentu > 3 ? false : 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..a5f79fb --- /dev/null +++ b/quiz/part1/soal2.js @@ -0,0 +1,38 @@ +//cari faktor persekutuan terbesar +function fpb(angka1, angka2) { + let fa1=[]; + let fa2=[]; + let res = 0 + for (let i = 0; i <= angka1; i++) { + if (angka1%i===0) { + fa1.push(i) + }; + }; + + + for (let j = 0; j <= angka2 ; j++) { + if (angka2%j===0) { + fa2.push(j) + }; + }; + + + + for (let x = 0; x < fa2.length; x++) { + for (let y = 0; y < fa2.length; y++) { + if (fa1[x]===fa2[y]) { + res = fa1[x] + } + }; + + }; + return res +}; + + +// 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..626e893 --- /dev/null +++ b/quiz/part1/soal3.js @@ -0,0 +1,21 @@ +function cariMedian(arr) { + let res = 0; + let parr= arr.length; + const sortarr = arr.sort((a,b)=>a-b); + if (arr.length%2===0) { + let n1 = sortarr[parr/2-1]; + let n2 = sortarr[parr/2]; + res = (n1/2)+(n2/2); + }else{ + let n = parr; + res = Math.floor(n/2+1) + }; + return res +}; + +// 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..a0503a5 --- /dev/null +++ b/quiz/part1/soal4.js @@ -0,0 +1,33 @@ +function cariModus(arr) { + if (arr.length === 0) { + return -1; + } + + let freq = {}; + let modus = 0; + let max = 0; + for (let i = 0; i < arr.length; i++) { + freq[arr[i]] = (freq[arr[i]] || 0) + 1; + if (freq[arr[i]] > max) { + max = freq[arr[i]]; + modus = arr[i]; + } + } + + if (max === 1 || max === arr.length) return -1; + + for (let i = 0; i < arr.length; i++) { + if (freq[arr[i]] === max) { + return arr[i]; + } + } + 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/part2/README.md b/quiz/part2/README.md index cb8f86d..c17061c 100644 --- a/quiz/part2/README.md +++ b/quiz/part2/README.md @@ -3,6 +3,7 @@ Sabar wkwk ## Soal 1 + ```js /* 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. @@ -35,6 +36,7 @@ console.log(digitPerkalianMinimum(1)); // 2 ``` ## Soal 2 + ```js //DILARANG MENGGUNAKAN METHOD SORT, PELAJARI ALGORITMA SORTING YANG ADA DI GOOGLE //saran sih pake bubblesort walau tidak efisien tapi bagus buat belajar sorting @@ -43,14 +45,15 @@ function urutkanAbjad(str) { } // 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' +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' ``` ## Soal 3 + ```js //TIPS: gunakan method toUpperCase() dan toLowerCase() function tukarBesarKecil(kalimat) { @@ -58,13 +61,15 @@ function tukarBesarKecil(kalimat) { } // 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" +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" ``` + ## Soal 4 + ```js /* 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. @@ -77,13 +82,21 @@ Spasi juga dianggap sebagai karakter */ function checkAB(num) { - // you can only write your code here! + for (let i = 0; i < num.length; i++) { + if (num[i] === "a") { + if (num[i + 4] === "b") return true; + } + if (num[i] === "b") { + if (num[i + 4] === "a") return true; + } + } + return false; } // 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 +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 +``` diff --git a/quiz/part2/soal1.js b/quiz/part2/soal1.js new file mode 100644 index 0000000..9b18a0e --- /dev/null +++ b/quiz/part2/soal1.js @@ -0,0 +1,29 @@ +function digitPerkalianMinimum(angka) { + let pasangan = [] + let hasil = 0 + + + for (let i = 0; i <= angka; i++) { + if (angka%i===0) { + hasil = angka/i + pasangan.push([i,hasil].join('')) + } + } + + pasangan.sort((a,b)=>a-b) + + + if (pasangan[0]==='11') { + return 2 + } + + + return pasangan[0].length +} + +// TEST CASES +console.log(digitPerkalianMinimum(24)); // 2 +console.log(digitPerkalianMinimum(90)); // 3 +console.log(digitPerkalianMinimum(20)); // 2 +console.log(digitPerkalianMinimum(179)); // 4 +console.log(digitPerkalianMinimum(1)); // 2 \ No newline at end of file diff --git a/quiz/part2/soal2.js b/quiz/part2/soal2.js new file mode 100644 index 0000000..ce70741 --- /dev/null +++ b/quiz/part2/soal2.js @@ -0,0 +1,20 @@ +//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) { + let clone = [...str] + for (let i = 0; i < clone.length-1; i++) { + for (let j = 0; j < clone.length-i; j++) { + if (clone[j]>clone[j+1]) { + [clone[j],clone[j+1]]=[clone[j+1],clone[j]] + } + } + } + return clone.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..ffd28bb --- /dev/null +++ b/quiz/part2/soal3.js @@ -0,0 +1,18 @@ +function tukarBesarKecil(kalimat) { + let res = '' + for (let i = 0; i < kalimat.length; i++) { + if (kalimat[i] === kalimat[i].toUpperCase()) { + res += kalimat[i].toLowerCase() + }else{ + res+= kalimat[i].toUpperCase() + } + } + return res +} + +// 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..34d6454 --- /dev/null +++ b/quiz/part2/soal4.js @@ -0,0 +1,19 @@ +function checkAB(str) { + let selisihIndex = 4; + for (let i = 0; i < str.length - selisihIndex; i++) { + if ( + (str[i] === "a" && str[i + selisihIndex] === "b") || + (str[i] === "b" && str[i + selisihIndex] === "a") + ) { + return true; + } + } + return false; +} + +// 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/README.md b/quiz/part3/README.md index be5fa03..279a79c 100644 --- a/quiz/part3/README.md +++ b/quiz/part3/README.md @@ -3,13 +3,17 @@ Akhirnya ketemu object ## Soal 1 + ```js function changeMe(arr) { // you can only write your code here! } // TEST CASES -changeMe([['Christ', 'Evans', 'Male', 1982], ['Robert', 'Downey', 'Male']]); // 1. Christ Evans: +changeMe([ + ["Christ", "Evans", "Male", 1982], + ["Robert", "Downey", "Male"], +]); // 1. Christ Evans: // Christ Evans: { firstName: 'Christ', // lastName: 'Evans', // gender: 'Male', @@ -25,6 +29,7 @@ changeMe([]); // "" ``` ## Soal 2 + ```js /* 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. @@ -53,28 +58,29 @@ function shoppingTime(memberId, money) { } // 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)); +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("", 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 ``` ## Soal 3 + ```js /* Toko X yang sedang melakukan SALE ingin menghitung jumlah profit untuk setiap jenis barang yang terjual pada hari itu. @@ -90,16 +96,23 @@ Function countProfit akan mengembalikan/me-return sebuah array of object dimana */ function countProfit(shoppers) { - var listBarang = [ ['Sepatu Stacattu', 1500000, 10], - ['Baju Zoro', 500000, 2], - ['Sweater Uniklooh', 175000, 1] - ]; + var listBarang = [ + ["Sepatu Stacattu", 1500000, 10], + ["Baju Zoro", 500000, 2], + ["Sweater Uniklooh", 175000, 1], + ]; // 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}])); +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, @@ -113,7 +126,15 @@ console.log(countProfit([{name: 'Windi', product: 'Sepatu Stacattu', amount: 2}, // 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}])); +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, @@ -126,7 +147,9 @@ console.log(countProfit([{name: 'Windi', product: 'Sepatu Stacattu', amount: 8}, // shoppers: [ 'Rani' ], // leftOver: 0, // totalProfit: 175000 } ] -console.log(countProfit([{name: 'Windi', product: 'Sepatu Naiki', amount: 5}])); +console.log( + countProfit([{ name: "Windi", product: "Sepatu Naiki", amount: 5 }]) +); // [ { product: 'Sepatu Stacattu', // shoppers: [], // leftOver: 10, @@ -140,4 +163,4 @@ console.log(countProfit([{name: 'Windi', product: 'Sepatu Naiki', amount: 5}])); // leftOver: 1, // totalProfit: 0 } ] console.log(countProfit([])); //[] -``` \ No newline at end of file +``` diff --git a/quiz/part3/soal1.js b/quiz/part3/soal1.js new file mode 100644 index 0000000..f7b70db --- /dev/null +++ b/quiz/part3/soal1.js @@ -0,0 +1,36 @@ +function changeMe(arr) { + if (arr==='') { + return '' + } + let bio = [] + + for (let i = 0; i < arr.length; i++) { + let obj = { + FirstName:arr[i][0], + LastName:arr[i][1], + Gender:arr[i][2], + age:arr[i][3]?2025-arr[i][3]:`Invalid Birth Year`, + } + + bio.push(obj) + } + + return bio + +} + +// TEST CASES +console.log(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. + +console.log(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..9902b4c --- /dev/null +++ b/quiz/part3/soal2.js @@ -0,0 +1,54 @@ +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"; + } + + const items = [ + { name: "Sepatu Stacattu", price: 1500000 }, + { name: "Baju Zoro", price: 500000 }, + { name: "Baju H&N", price: 250000 }, + { name: "Sweater Uniklooh", price: 175000 }, + { name: "Casing Handphone", price: 50000 }, + ]; + + let listPurchased = []; + let changeMoney = money; + + for (let i = 0; i < items.length; i++) { + if (changeMoney >= items[i].price) { + listPurchased.push(items[i].name); + changeMoney -= items[i].price; + } + } + + return { + memberId: memberId, + money: money, + listPurchased: listPurchased, + changeMoney: changeMoney, + }; +} + +// 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..259bd41 --- /dev/null +++ b/quiz/part3/soal3.js @@ -0,0 +1,79 @@ + +function countProfit(shoppers) { + let res = [] + // cek jika kosong + if (shoppers.length===0) { + return [] + } + // list barang + var listBarang = [ ['Sepatu Stacattu', 1500000, 10], + ['Baju Zoro', 500000, 2], + ['Sweater Uniklooh', 175000, 1] + ]; + +for (let i = 0; i < listBarang.length; i++) { + let obj = { + product: listBarang[i][0], + shoppers: [], + leftOver: listBarang[i][2], + totalProfit: 0 + } + + for (let j = 0; j < shoppers.length; j++) { + if(shoppers[j].product===listBarang[i][0]){ + if (obj.leftOverres[cls].score) { + res[cls]={name:name,score:score} + } + } + return res +} + +// 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 } +// } \ No newline at end of file diff --git a/quiz/ujian/soal4.js b/quiz/ujian/soal4.js new file mode 100644 index 0000000..f637226 --- /dev/null +++ b/quiz/ujian/soal4.js @@ -0,0 +1,121 @@ +/* +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) { + let res = {} + // cek jika kosong + if(students.length===0){ + return {} + } + for (let i = 0; i < students.length; i++) { + let cls = students[i].class + let score = students[i].score + let name = students[i].name + // cek jika tidak ada class di dalam res + if(!res[cls]){ + res[cls]=[] + } + + // cek nilai + if (score>75) { + res[cls].push({name:name,score:score}) + } + } + return res +} + +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('\n') +// { +// 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