Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 0 additions & 78 deletions quiz/part1/README.md

This file was deleted.

17 changes: 17 additions & 0 deletions quiz/part1/soal1.js
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions quiz/part1/soal2.js
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions quiz/part1/soal3.js
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions quiz/part1/soal4.js
Original file line number Diff line number Diff line change
@@ -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
47 changes: 30 additions & 17 deletions quiz/part2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -43,28 +45,31 @@ 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) {
// you can only write your code here!
}

// 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.
Expand All @@ -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
```
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
```
29 changes: 29 additions & 0 deletions quiz/part2/soal1.js
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions quiz/part2/soal2.js
Original file line number Diff line number Diff line change
@@ -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'
18 changes: 18 additions & 0 deletions quiz/part2/soal3.js
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 19 additions & 0 deletions quiz/part2/soal4.js
Original file line number Diff line number Diff line change
@@ -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
Loading