From 8916e81a874e22c3d1cc89d7a16610787e8ba77c Mon Sep 17 00:00:00 2001 From: nazril27 Date: Wed, 26 Mar 2025 16:30:15 +0700 Subject: [PATCH 1/3] bekap dulu --- quiz/part1/soal1.js | 19 +++++++++ quiz/part1/soal2.js | 14 ++++++ quiz/part1/soal3.js | 20 +++++++++ quiz/part1/soal4.js | 24 +++++++++++ quiz/part1/soal5.js | 26 ++++++++++++ quiz/part2/soal1.js | 28 ++++++++++++ quiz/part2/soal2.js | 22 ++++++++++ quiz/part2/soal3.js | 37 ++++++++++++++++ quiz/part2/soal4.js | 25 +++++++++++ quiz/part3/soal1.js | 18 ++++++++ quiz/part3/soal2.js | 46 ++++++++++++++++++++ quiz/part3/soal3.js | 64 ++++++++++++++++++++++++++++ quiz/ujian/soal1.js | 58 +++++++++++++++++++++++++ quiz/ujian/soal2.js | 19 +++++++++ quiz/ujian/soal3.js | 70 ++++++++++++++++++++++++++++++ quiz/ujian/soal4.js | 101 ++++++++++++++++++++++++++++++++++++++++++++ 16 files changed, 591 insertions(+) create mode 100644 quiz/part1/soal1.js create mode 100644 quiz/part1/soal2.js create mode 100644 quiz/part1/soal3.js create mode 100644 quiz/part1/soal4.js create mode 100644 quiz/part1/soal5.js create mode 100644 quiz/part2/soal1.js create mode 100644 quiz/part2/soal2.js create mode 100644 quiz/part2/soal3.js create mode 100644 quiz/part2/soal4.js create mode 100644 quiz/part3/soal1.js create mode 100644 quiz/part3/soal2.js create mode 100644 quiz/part3/soal3.js create mode 100644 quiz/ujian/soal1.js create mode 100644 quiz/ujian/soal2.js create mode 100644 quiz/ujian/soal3.js create mode 100644 quiz/ujian/soal4.js diff --git a/quiz/part1/soal1.js b/quiz/part1/soal1.js new file mode 100644 index 0000000..773488a --- /dev/null +++ b/quiz/part1/soal1.js @@ -0,0 +1,19 @@ +//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! + let isPrime = 0; + for (let i = 0; i <= angka; i++){ + if(angka % i === 0){ + isPrime++; + } + } + return isPrime === 2; + } + + // 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..9fe9d6a --- /dev/null +++ b/quiz/part1/soal2.js @@ -0,0 +1,14 @@ +//cari faktor persekutuan terbesar +function fpb(angka1, angka2) { + // you can only write your code here! + let i = 1; + + + } + + // 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..259a68c --- /dev/null +++ b/quiz/part1/soal3.js @@ -0,0 +1,20 @@ +function cariMedian(arr) { + // you can only write your code here! + const rumusGenap = (arr.length - 1) / 2; + const rumusGanjil = arr.length - 1; + const uhuu = (rumusGanjil + 1) / 2; + // if (arr.length % 2 === 0){ + // return (arr[rumusGenap] + arr[rumusGenap + 1]) / 2; + // } else { + // return arr[rumusGanjil] / 2; + // } + return arr[uhuu]; + + } + + // 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..aa62399 --- /dev/null +++ b/quiz/part1/soal4.js @@ -0,0 +1,24 @@ +/* +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! + let modus = []; + let cekPerbedaan = 0; + for (let i = 0; i < arr.length; i++){ + for (let j = 0; j < arr.length; j++){ + if(i !== j && arr[i] === arr[j]){ + cekPerbedaan++; + modus.push(arr[i]); + } + } + } + return cekPerbedaan; + } + + // 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..2859f1b --- /dev/null +++ b/quiz/part1/soal5.js @@ -0,0 +1,26 @@ +//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 index = []; + let result = ''; + let alfabet = 'abcdefghijklmnopqrstuvwxyz'; + for (let i = 0; i < kata.length; i++){ + for (let j = 0; j < alfabet.length; j++){ + if(kata[i] === alfabet[j]){ + index.push(j + 1); + } + } + } + for (let k = 0; k < index.length; k++){ + result += alfabet[index[k]]; + } + return result; + } + + // 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..312f128 --- /dev/null +++ b/quiz/part2/soal1.js @@ -0,0 +1,28 @@ +/* +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) { + // you can only write your code here! + } + + // 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..264c777 --- /dev/null +++ b/quiz/part2/soal2.js @@ -0,0 +1,22 @@ +//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! + const alfabet = 'abcdefghijklmnopqrstuvwxyz'; + let order = ''; + for (let i = 0; i < alfabet.length; i++){ + for (let j = 0; j < str.length; j++){ + if(alfabet[i] === str[j]){ + order += alfabet[i]; + } + } + } + return order; + } + + // 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..1eccbae --- /dev/null +++ b/quiz/part2/soal3.js @@ -0,0 +1,37 @@ +//TIPS: gunakan method toUpperCase() dan toLowerCase() +function tukarBesarKecil(kalimat) { + // you can only write your code here! + let arr = []; + let result = ''; + const alfabet = 'abcdefghijklmnopqrstuvwxyz1234567890'; + const hurufBadag = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ-/?.<>,;[]=+-_{}$!@#%^&*()'; + + for (let i = 0; i < kalimat.length; i++){ + for (let j = 0; j < alfabet.length; j++){ + if (kalimat[i] === alfabet[j]){ + arr[i] = kalimat[i].toUpperCase(); + } + } + } + + for (let k = 0; k < kalimat.length; k++){ + for (let l = 0; l < hurufBadag.length; l++){ + if(kalimat[k] === hurufBadag[l]){ + arr[k] = kalimat[k].toLowerCase(); + } + } + } + + for (let m = 0; m < arr.length; m++){ + result += arr[m]; + } + + return result; + } + + // 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..3927b36 --- /dev/null +++ b/quiz/part2/soal4.js @@ -0,0 +1,25 @@ +/* +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(num) { + // you can only write your code here! + let dexnum = []; + for (let i = 0; i < num.length; i++){ + + } + return dexnum; + } + + // 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..41fac42 --- /dev/null +++ b/quiz/part3/soal1.js @@ -0,0 +1,18 @@ +function changeMe(arr) { + // you can only write your code here! + } + + // 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..8ab7cc2 --- /dev/null +++ b/quiz/part3/soal2.js @@ -0,0 +1,46 @@ +/* +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! + } + + // 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..5995a1a --- /dev/null +++ b/quiz/part3/soal3.js @@ -0,0 +1,64 @@ +/* +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] + ]; + + // 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..6142a48 --- /dev/null +++ b/quiz/ujian/soal1.js @@ -0,0 +1,58 @@ +/* +Implementasikan function deepSum untuk mendapatkan jumlah pertambahan dari angka-angka yang terdapat di dalam array +*/ + +function deepSum (arr) { + // Code disini + let sum = 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++){ + sum += arr[i][j][k]; + } + } + } + return sum; + } + + //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..7e79e53 --- /dev/null +++ b/quiz/ujian/soal2.js @@ -0,0 +1,19 @@ +/* +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) { + rute = ['A', 'B', 'C', 'D', 'E', 'F']; + //your code here + } + + //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..a4a0397 --- /dev/null +++ b/quiz/ujian/soal3.js @@ -0,0 +1,70 @@ +function highestScore (students) { + // Code disini + } + + // 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..edda51a --- /dev/null +++ b/quiz/ujian/soal4.js @@ -0,0 +1,101 @@ +/* +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 + } + + 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 From 6682c74db777c836a15a3b6a33cf017cf951f21c Mon Sep 17 00:00:00 2001 From: nazril27 Date: Thu, 10 Apr 2025 20:25:43 +0700 Subject: [PATCH 2/3] bek ap --- quiz/part1/soal2.js | 16 +++++++++++++--- quiz/part1/soal3.js | 25 ++++++++++++++++--------- quiz/part1/soal4.js | 18 +++++++++--------- quiz/part2/soal1.js | 1 + quiz/part2/soal4.js | 10 ++++++---- quiz/part3/soal1.js | 16 ++++++++++++++++ quiz/part3/soal2.js | 32 ++++++++++++++++++++++++++++++++ quiz/part3/soal3.js | 33 ++++++++++++++++++++++++++++++++- quiz/ujian/soal2.js | 27 +++++++++++++++++++++++++++ quiz/ujian/soal3.js | 11 +++++++++++ 10 files changed, 163 insertions(+), 26 deletions(-) diff --git a/quiz/part1/soal2.js b/quiz/part1/soal2.js index 9fe9d6a..0559f3e 100644 --- a/quiz/part1/soal2.js +++ b/quiz/part1/soal2.js @@ -1,9 +1,19 @@ //cari faktor persekutuan terbesar function fpb(angka1, angka2) { // you can only write your code here! - let i = 1; - - + let arr = []; + let siapaYangGede; + if(angka1 > angka2){ + siapaYangGede = angka1; + } else { + siapaYangGede = angka2; + } + for(let i = 1; i <= siapaYangGede; i++){ + if(angka1 % i === 0 && angka2 % i === 0){ + arr.push(i); + } + } + return arr[arr.length - 1]; } // TEST CASES diff --git a/quiz/part1/soal3.js b/quiz/part1/soal3.js index 259a68c..a0bb17c 100644 --- a/quiz/part1/soal3.js +++ b/quiz/part1/soal3.js @@ -1,15 +1,22 @@ function cariMedian(arr) { // you can only write your code here! - const rumusGenap = (arr.length - 1) / 2; - const rumusGanjil = arr.length - 1; - const uhuu = (rumusGanjil + 1) / 2; - // if (arr.length % 2 === 0){ - // return (arr[rumusGenap] + arr[rumusGenap + 1]) / 2; - // } else { - // return arr[rumusGanjil] / 2; - // } - return arr[uhuu]; + let cekMed; + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr.length - 1; j++) { + if (arr[j] > arr[j + 1]) { + [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; + } + } + } + const median = arr.length / 2; + if(arr.length % 2 === 0){ + arr = (arr[median] + arr[(median - 1)]) / 2; + } else { + arr = arr[Math.floor(median)]; + } + + return arr; } // TEST CASES diff --git a/quiz/part1/soal4.js b/quiz/part1/soal4.js index aa62399..af64393 100644 --- a/quiz/part1/soal4.js +++ b/quiz/part1/soal4.js @@ -3,17 +3,15 @@ Diberikan sebuah function cariModus(arr) yang menerima sebuah array angka. Funct */ function cariModus(arr) { // you can only write your code here! - let modus = []; - let cekPerbedaan = 0; - for (let i = 0; i < arr.length; i++){ - for (let j = 0; j < arr.length; j++){ - if(i !== j && arr[i] === arr[j]){ - cekPerbedaan++; - modus.push(arr[i]); + let sortArr = arr; + for(let i = 0; i < sortArr.length; i++){ + for(let j = 0; j < sortArr.length - 1; j++){ + if(sortArr[j] > sortArr[j + 1]){ + [sortArr[j], sortArr[j + 1]] = [sortArr[j + 1], sortArr[j]]; } } } - return cekPerbedaan; + return arr; } // TEST CASES @@ -21,4 +19,6 @@ function cariModus(arr) { 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 + console.log(cariModus([7, 7, 7, 7, 7])); // -1 + + // console.log(cariModus([7, 7, 7, 7])); \ No newline at end of file diff --git a/quiz/part2/soal1.js b/quiz/part2/soal1.js index 312f128..cdd493d 100644 --- a/quiz/part2/soal1.js +++ b/quiz/part2/soal1.js @@ -18,6 +18,7 @@ Return 2 */ function digitPerkalianMinimum(angka) { // you can only write your code here! + } // TEST CASES diff --git a/quiz/part2/soal4.js b/quiz/part2/soal4.js index 3927b36..aed3221 100644 --- a/quiz/part2/soal4.js +++ b/quiz/part2/soal4.js @@ -10,11 +10,13 @@ Spasi juga dianggap sebagai karakter function checkAB(num) { // you can only write your code here! - let dexnum = []; - for (let i = 0; i < num.length; i++){ - + let result = false; + for(let i = 0; i < num.length; i++){ + if((num[i] === 'a' && num[i + 4] === 'b') || (num[i] === 'b' && num[i + 4] === 'a')){ + result = true; + } } - return dexnum; + return result; } // TEST CASES diff --git a/quiz/part3/soal1.js b/quiz/part3/soal1.js index 41fac42..6fc983c 100644 --- a/quiz/part3/soal1.js +++ b/quiz/part3/soal1.js @@ -1,5 +1,21 @@ function changeMe(arr) { // you can only write your code here! + let firstName, lastName, gender, age; + for(let i = 0; i < arr.length; i++){ + firstName = arr[i][0]; + lastName = arr[i][1]; + gender = arr[i][2]; + if(!arr[i][3]){ + age = 'Invalid Birth Year'; + } else { + age = 2025 - arr[i][3]; + } + let person = { + [`${firstName} ${lastName}`]: + {firstName: firstName, lastName: lastName, gender: gender, age: age} + } + console.log(person); + } } // TEST CASES diff --git a/quiz/part3/soal2.js b/quiz/part3/soal2.js index 8ab7cc2..b24f271 100644 --- a/quiz/part3/soal2.js +++ b/quiz/part3/soal2.js @@ -22,7 +22,39 @@ maka output: function shoppingTime(memberId, money) { // you can only write your code here! + let buyer = {}; + let brand = []; + + if(!memberId){ + return 'Mohon maaf, toko X hanya berlaku untuk member saja'; + } + if(money < 50000){ + return 'Mohon maaf, uang tidak cukup'; } + + const price = { + 'Sepatu brand Stacattu': 1500000, + 'Baju brand Zoro': 500000, + 'Baju brand H&N': 250000, + 'Sweater brand Uniklooh': 175000, + 'Casing Handphone': 50000 + } + + buyer.memberId = memberId; + buyer.money = money; + + for(let key in price){ + if(money >= price[key]){ + brand.push(key); + money -= price[key]; + } + } + + buyer.listPurchased = brand; + buyer.changeMoney = money; + + return buyer; +} // TEST CASES console.log(shoppingTime('1820RzKrnWn08', 2475000)); diff --git a/quiz/part3/soal3.js b/quiz/part3/soal3.js index 5995a1a..387d127 100644 --- a/quiz/part3/soal3.js +++ b/quiz/part3/soal3.js @@ -18,6 +18,37 @@ function countProfit(shoppers) { ]; // you can only write your code here! + let gruop = []; + let sepatu = []; + let nama; + let produk; + let sisa; + let harga; + + for(let t = 0; t < listBarang.length; t++){ + produk = listBarang[t][0]; + sisa = listBarang[t][2]; + harga = listBarang[t][1]; + for(let i = 0; i < shoppers.length; i++){ + for(let key in shoppers[i]){ + nama = shoppers[i].name; + beli = shoppers[i].amount; + + + } + if(produk === shoppers[i].product && sisa >= beli){ + sepatu.push(nama); + sisa -= beli; + harga *= beli; + } + // gruop.push(sepatu); + nama = undefined; + // sepatu = []; + } + gruop.push(sepatu); + sepatu = []; + } + return harga; } // TEST CASES @@ -61,4 +92,4 @@ function countProfit(shoppers) { // shoppers: [], // leftOver: 1, // totalProfit: 0 } ] - console.log(countProfit([])); //[] \ No newline at end of file + // console.log(countProfit([])); //[] \ No newline at end of file diff --git a/quiz/ujian/soal2.js b/quiz/ujian/soal2.js index 7e79e53..322d0c1 100644 --- a/quiz/ujian/soal2.js +++ b/quiz/ujian/soal2.js @@ -9,6 +9,33 @@ Contoh: input: [['Dimitri', 'B', 'F']] output: [{ penumpang: 'Dimitri', naikDari function naikAngkot(arrPenumpang) { rute = ['A', 'B', 'C', 'D', 'E', 'F']; //your code here + let obj = {}; + let jarak; + let p; + let q; + + + let arr = []; + for(let i = 0; i < arrPenumpang.length; i++){ + obj.penumpang = arrPenumpang[i][0]; + obj.naikDari = arrPenumpang[i][1]; + obj.tujuan = arrPenumpang[i][2]; + obj.bayar; + arr.push(obj); + obj = {}; + } + + for(let i = 0; i < arrPenumpang.length; i++){ + for(let j = 0; j < arrPenumpang[i].length; j++){ + for(let k = 0; k < rute.length; k++){ + if(arrPenumpang[i][j] === rute[k]){ + p = k; + q = k + } + } + } + } + return jarak; } //TEST CASE diff --git a/quiz/ujian/soal3.js b/quiz/ujian/soal3.js index a4a0397..4f39d15 100644 --- a/quiz/ujian/soal3.js +++ b/quiz/ujian/soal3.js @@ -1,5 +1,16 @@ function highestScore (students) { // Code disini + let array = []; + let arr = []; + + for(let i = 0; i < students.length; i++){ + for(let key in students[i]){ + arr.push(students[i][key]); + } + array.push(arr); + arr = []; + } + return array; } // TEST CASE From 0e8c6c41d975b19810baf0e3b902be246e5a6244 Mon Sep 17 00:00:00 2001 From: nazril57 Date: Wed, 23 Apr 2025 00:30:49 +0700 Subject: [PATCH 3/3] beres juga njir --- quiz/part1/soal1.js | 8 +-- quiz/part1/soal2.js | 15 ++-- quiz/part1/soal3.js | 6 +- quiz/part1/soal4.js | 31 ++++++--- quiz/part1/soal5.js | 18 +++-- quiz/part2/soal1.js | 22 +++++- quiz/part2/soal2.js | 2 + quiz/part2/soal3.js | 33 ++++----- quiz/part3/soal1.js | 8 +-- quiz/part3/soal2.js | 8 +-- quiz/part3/soal3.js | 66 ++++++++++-------- quiz/ujian/soal1.js | 5 ++ quiz/ujian/soal2.js | 40 +++++------ quiz/ujian/soal3.js | 162 +++++++++++++++++++++++--------------------- quiz/ujian/soal4.js | 16 +++++ 15 files changed, 255 insertions(+), 185 deletions(-) diff --git a/quiz/part1/soal1.js b/quiz/part1/soal1.js index 773488a..4ab2905 100644 --- a/quiz/part1/soal1.js +++ b/quiz/part1/soal1.js @@ -2,13 +2,13 @@ //cek google bagi yang ga tau apa itu angka prima function angkaPrima(angka) { // you can only write your code here! - let isPrime = 0; - for (let i = 0; i <= angka; i++){ + let hitung = 0; + for (let i = 1; i <= angka; i++){ if(angka % i === 0){ - isPrime++; + hitung++; } } - return isPrime === 2; + return hitung === 2; } // TEST CASES diff --git a/quiz/part1/soal2.js b/quiz/part1/soal2.js index 0559f3e..6f4ec87 100644 --- a/quiz/part1/soal2.js +++ b/quiz/part1/soal2.js @@ -1,19 +1,20 @@ //cari faktor persekutuan terbesar function fpb(angka1, angka2) { // you can only write your code here! - let arr = []; - let siapaYangGede; + let gedeMana, result; + if(angka1 > angka2){ - siapaYangGede = angka1; + gedeMana = angka1; } else { - siapaYangGede = angka2; + gedeMana = angka2; } - for(let i = 1; i <= siapaYangGede; i++){ + + for(let i = 1; i <= gedeMana; i++){ if(angka1 % i === 0 && angka2 % i === 0){ - arr.push(i); + result = i; } } - return arr[arr.length - 1]; + return result; } // TEST CASES diff --git a/quiz/part1/soal3.js b/quiz/part1/soal3.js index a0bb17c..8f0a441 100644 --- a/quiz/part1/soal3.js +++ b/quiz/part1/soal3.js @@ -1,7 +1,7 @@ function cariMedian(arr) { // you can only write your code here! - - let cekMed; + const median = arr.length / 2; + for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr.length - 1; j++) { if (arr[j] > arr[j + 1]) { @@ -9,7 +9,7 @@ function cariMedian(arr) { } } } - const median = arr.length / 2; + if(arr.length % 2 === 0){ arr = (arr[median] + arr[(median - 1)]) / 2; } else { diff --git a/quiz/part1/soal4.js b/quiz/part1/soal4.js index af64393..715e40f 100644 --- a/quiz/part1/soal4.js +++ b/quiz/part1/soal4.js @@ -3,16 +3,28 @@ Diberikan sebuah function cariModus(arr) yang menerima sebuah array angka. Funct */ function cariModus(arr) { // you can only write your code here! - let sortArr = arr; - for(let i = 0; i < sortArr.length; i++){ - for(let j = 0; j < sortArr.length - 1; j++){ - if(sortArr[j] > sortArr[j + 1]){ - [sortArr[j], sortArr[j + 1]] = [sortArr[j + 1], sortArr[j]]; - } - } + let result = -1; + let obj = {}; + let modus = 1; + + for (let i = 0; i < arr.length; i++) { + let key = ` ${arr[i]} `; + if (!obj[key]) { + obj[key] = 1; + } else { + obj[key] += 1; + } + } + + for (let key in obj) { + if (modus < obj[key] && obj[key] !== arr.length) { + modus = obj[key]; + result = key; } - return arr; } + + return Number(result); +} // TEST CASES console.log(cariModus([10, 4, 5, 2, 4])); // 4 @@ -20,5 +32,4 @@ function cariModus(arr) { 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 - - // console.log(cariModus([7, 7, 7, 7])); \ No newline at end of file + \ No newline at end of file diff --git a/quiz/part1/soal5.js b/quiz/part1/soal5.js index 2859f1b..dd626cd 100644 --- a/quiz/part1/soal5.js +++ b/quiz/part1/soal5.js @@ -2,19 +2,23 @@ //intinya ubah huruf menjadi huruf setelahnya function ubahHuruf(kata) { // you can only write your code here! - let index = []; + const alfabet = 'abcdefghijklmnopqrstuvwxyz'; let result = ''; - let alfabet = 'abcdefghijklmnopqrstuvwxyz'; + for (let i = 0; i < kata.length; i++){ for (let j = 0; j < alfabet.length; j++){ + if(kata[i] === alfabet[j]){ - index.push(j + 1); + if(alfabet[j + 1] === undefined){ + result += alfabet[j - j]; + } else { + result += alfabet[j + 1]; + } + } } } - for (let k = 0; k < index.length; k++){ - result += alfabet[index[k]]; - } + return result; } @@ -23,4 +27,4 @@ function ubahHuruf(kata) { 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 + console.log(ubahHuruf('semangat')); // tfnbohbu diff --git a/quiz/part2/soal1.js b/quiz/part2/soal1.js index cdd493d..4b2525c 100644 --- a/quiz/part2/soal1.js +++ b/quiz/part2/soal1.js @@ -18,7 +18,27 @@ Return 2 */ function digitPerkalianMinimum(angka) { // you can only write your code here! - + let mid, beforeMid; + let result = ''; + let arr = []; + + for (let i = 0; i <= angka; i++) { + if (angka % i === 0) { + arr.push(i); + } + } + + if (arr.length === 1) { + return 2; + } else if (arr.length === 3) { + result += `${arr[0]}${arr[arr.length - 1]}`; + return result.length; + } else { + mid = arr.length / 2; + beforeMid = mid - 1; + result += `${arr[mid]}${arr[beforeMid]}`; + return result.length; + } } // TEST CASES diff --git a/quiz/part2/soal2.js b/quiz/part2/soal2.js index 264c777..ce2de5d 100644 --- a/quiz/part2/soal2.js +++ b/quiz/part2/soal2.js @@ -4,6 +4,7 @@ function urutkanAbjad(str) { // you can only write your code here! const alfabet = 'abcdefghijklmnopqrstuvwxyz'; let order = ''; + for (let i = 0; i < alfabet.length; i++){ for (let j = 0; j < str.length; j++){ if(alfabet[i] === str[j]){ @@ -11,6 +12,7 @@ function urutkanAbjad(str) { } } } + return order; } diff --git a/quiz/part2/soal3.js b/quiz/part2/soal3.js index 1eccbae..50bf96a 100644 --- a/quiz/part2/soal3.js +++ b/quiz/part2/soal3.js @@ -1,31 +1,26 @@ //TIPS: gunakan method toUpperCase() dan toLowerCase() function tukarBesarKecil(kalimat) { // you can only write your code here! - let arr = []; + const alfabet = 'abcdefghijklmnopqrstuvwxyz'; + const notAlfabet = '1234567890!@#$%^&*()- '; let result = ''; - const alfabet = 'abcdefghijklmnopqrstuvwxyz1234567890'; - const hurufBadag = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ-/?.<>,;[]=+-_{}$!@#%^&*()'; - for (let i = 0; i < kalimat.length; i++){ - for (let j = 0; j < alfabet.length; j++){ - if (kalimat[i] === alfabet[j]){ - arr[i] = kalimat[i].toUpperCase(); - } - } - } - - for (let k = 0; k < kalimat.length; k++){ - for (let l = 0; l < hurufBadag.length; l++){ - if(kalimat[k] === hurufBadag[l]){ - arr[k] = kalimat[k].toLowerCase(); + for (let i = 0; i < kalimat.length; i++) { + for (let j = 0; j < alfabet.length; j++) { + + if (kalimat[i] === alfabet[j].toUpperCase()) { + result += kalimat[i].toLowerCase(); } + if (kalimat[i] === alfabet[j]) { + result += kalimat[i].toUpperCase(); + } + if (kalimat[i] === notAlfabet[j]) { + result += kalimat[i]; + } + } } - for (let m = 0; m < arr.length; m++){ - result += arr[m]; - } - return result; } diff --git a/quiz/part3/soal1.js b/quiz/part3/soal1.js index 6fc983c..997b3a8 100644 --- a/quiz/part3/soal1.js +++ b/quiz/part3/soal1.js @@ -1,10 +1,10 @@ function changeMe(arr) { // you can only write your code here! - let firstName, lastName, gender, age; for(let i = 0; i < arr.length; i++){ - firstName = arr[i][0]; - lastName = arr[i][1]; - gender = arr[i][2]; + let firstName = arr[i][0]; + let lastName = arr[i][1]; + let gender = arr[i][2]; + let age; if(!arr[i][3]){ age = 'Invalid Birth Year'; } else { diff --git a/quiz/part3/soal2.js b/quiz/part3/soal2.js index b24f271..f5b73d6 100644 --- a/quiz/part3/soal2.js +++ b/quiz/part3/soal2.js @@ -33,10 +33,10 @@ function shoppingTime(memberId, money) { } const price = { - 'Sepatu brand Stacattu': 1500000, - 'Baju brand Zoro': 500000, - 'Baju brand H&N': 250000, - 'Sweater brand Uniklooh': 175000, + 'Sepatu Stacattu': 1500000, + 'Baju Zoro': 500000, + 'Baju H&N': 250000, + 'Sweater Uniklooh': 175000, 'Casing Handphone': 50000 } diff --git a/quiz/part3/soal3.js b/quiz/part3/soal3.js index 387d127..6f5150d 100644 --- a/quiz/part3/soal3.js +++ b/quiz/part3/soal3.js @@ -18,38 +18,46 @@ function countProfit(shoppers) { ]; // you can only write your code here! - let gruop = []; - let sepatu = []; - let nama; - let produk; - let sisa; - let harga; + if (shoppers.length === 0) { + return shoppers; + } + + let result = []; + let konsumen, obj; + let namaPembeli, produk, stok, harga, jmlMembeli, membeliProduk, totalTerjual; + + for(let i = 0; i < listBarang.length; i++){ + produk = listBarang[i][0]; + harga = listBarang[i][1]; + stok = listBarang[i][2]; + konsumen = []; + totalTerjual = 0; - for(let t = 0; t < listBarang.length; t++){ - produk = listBarang[t][0]; - sisa = listBarang[t][2]; - harga = listBarang[t][1]; - for(let i = 0; i < shoppers.length; i++){ - for(let key in shoppers[i]){ - nama = shoppers[i].name; - beli = shoppers[i].amount; - + for(let j = 0; j < shoppers.length; j++){ + obj = {}; + for(let key in shoppers[j]){ + namaPembeli = shoppers[j].name; + jmlMembeli = shoppers[j].amount; + membeliProduk = shoppers[j].product; + } + if(produk === membeliProduk && stok >= jmlMembeli){ + stok -= jmlMembeli; + totalTerjual += jmlMembeli; + konsumen.push(namaPembeli); + } + + obj.product = produk; + obj.shoppers = konsumen; + obj.leftOver = stok; + obj.totalProfit = harga * totalTerjual; } - if(produk === shoppers[i].product && sisa >= beli){ - sepatu.push(nama); - sisa -= beli; - harga *= beli; - } - // gruop.push(sepatu); - nama = undefined; - // sepatu = []; - } - gruop.push(sepatu); - sepatu = []; - } - return harga; + + result.push(obj); } + + return result; +} // 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}])); @@ -92,4 +100,4 @@ function countProfit(shoppers) { // shoppers: [], // leftOver: 1, // totalProfit: 0 } ] - // console.log(countProfit([])); //[] \ No newline at end of file + console.log(countProfit([])); //[] \ No newline at end of file diff --git a/quiz/ujian/soal1.js b/quiz/ujian/soal1.js index 6142a48..9adb03f 100644 --- a/quiz/ujian/soal1.js +++ b/quiz/ujian/soal1.js @@ -4,6 +4,11 @@ Implementasikan function deepSum untuk mendapatkan jumlah pertambahan dari angka function deepSum (arr) { // Code disini + + if (arr.length === 0) { + return 'No number'; + } + let sum = 0; for (let i = 0; i < arr.length; i++){ for (let j = 0; j < arr[i].length; j++){ diff --git a/quiz/ujian/soal2.js b/quiz/ujian/soal2.js index 322d0c1..02f3ac1 100644 --- a/quiz/ujian/soal2.js +++ b/quiz/ujian/soal2.js @@ -9,33 +9,33 @@ Contoh: input: [['Dimitri', 'B', 'F']] output: [{ penumpang: 'Dimitri', naikDari function naikAngkot(arrPenumpang) { rute = ['A', 'B', 'C', 'D', 'E', 'F']; //your code here + let titikAkhir, titikAwal; + let arr = []; let obj = {}; - let jarak; - let p; - let q; + for (let i = 0; i < arrPenumpang.length; i++){ + for (let j = 0; j < arrPenumpang[i].length; j++){ + obj.penumpang = arrPenumpang[i][0]; + obj.naikDari = arrPenumpang[i][1]; + obj.tujuan = arrPenumpang[i][2]; - let arr = []; - for(let i = 0; i < arrPenumpang.length; i++){ - obj.penumpang = arrPenumpang[i][0]; - obj.naikDari = arrPenumpang[i][1]; - obj.tujuan = arrPenumpang[i][2]; - obj.bayar; - arr.push(obj); - obj = {}; - } - - for(let i = 0; i < arrPenumpang.length; i++){ - for(let j = 0; j < arrPenumpang[i].length; j++){ - for(let k = 0; k < rute.length; k++){ - if(arrPenumpang[i][j] === rute[k]){ - p = k; - q = k + for (let k = 0; k < rute.length; k++){ + if (obj.naikDari === rute[k]) { + titikAwal = k; + } + if (obj.tujuan === rute[k]) { + titikAkhir = k; } } + } + + titikAkhir -= titikAwal; + obj.ongkos = titikAkhir * 2000; + arr.push(obj); + obj = {}; } - return jarak; + return arr; } //TEST CASE diff --git a/quiz/ujian/soal3.js b/quiz/ujian/soal3.js index 4f39d15..484cc69 100644 --- a/quiz/ujian/soal3.js +++ b/quiz/ujian/soal3.js @@ -1,81 +1,89 @@ function highestScore (students) { - // Code disini - let array = []; - let arr = []; + // Code disini + let result = {}; - for(let i = 0; i < students.length; i++){ - for(let key in students[i]){ - arr.push(students[i][key]); + for (let i = 0; i < students.length - 1; i++) { + for (let j = i + 1; j < students.length; j++) { + if (students[i].class === students[j].class && students[i].score > students[j].score) { + students[j] = false; + } + } + } + + for (let i = 0; i < students.length; i++) { + if (students[i]) { + result[students[i].class] = { + name : students[i].name, + score : students[i].score + }; } - array.push(arr); - arr = []; - } - return array; } - - // 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 + 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 index edda51a..c632b88 100644 --- a/quiz/ujian/soal4.js +++ b/quiz/ujian/soal4.js @@ -21,6 +21,22 @@ Output yang diharapkan berupa Object dengan format sebagai berikut: function graduates (students) { // Code disini + let result = {}; + + for (let i = 0; i < students.length; i++) { + let arr = []; + for (let j = 0; j < students.length; j++) { + if (students[i].class === students[j].class && students[j].score > 75) { + let obj = { + name : students[j].name, + score : students[j].score + } + arr.push(obj); + } + } + result[students[i].class] = arr; + } + return result; } console.log(graduates([