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
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
16 changes: 16 additions & 0 deletions quiz/part1/Part1 Soal1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function angkaPrima(angka) {
if (angka < 2) return false;

for (let i = 2; i <= Math.sqrt(angka); i++) {
if (angka % i === 0) {
return false;
}
}
return true;
}

console.log(angkaPrima(3));
console.log(angkaPrima(7));
console.log(angkaPrima(6));
console.log(angkaPrima(23));
console.log(angkaPrima(33));
14 changes: 14 additions & 0 deletions quiz/part1/Part1 Soal2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function fpb(angka1, angka2) {
while (angka2 !== 0) {
let sisa = angka1 % angka2;
angka1 = angka2;
angka2 = sisa;
}
return angka1;
}

console.log(fpb(12, 16));
console.log(fpb(50, 40));
console.log(fpb(22, 99));
console.log(fpb(24, 36));
console.log(fpb(17, 23));
18 changes: 18 additions & 0 deletions quiz/part1/Part1 Soal3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function cariMedian(arr) {
arr.sort((a, b) => a - b);

let panjang = arr.length;
let tengah = Math.floor(panjang / 2);

if (panjang % 2 === 1) {
return arr[tengah];
} else {
return (arr[tengah - 1] + arr[tengah]) / 2;
}
}

console.log(cariMedian([1, 2, 3, 4, 5]));
console.log(cariMedian([1, 3, 4, 10, 12, 13]));
console.log(cariMedian([3, 4, 7, 6, 10]));
console.log(cariMedian([1, 3, 3]));
console.log(cariMedian([7, 7, 8, 8]));
37 changes: 37 additions & 0 deletions quiz/part1/Part1 Soal4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function cariModus(arr) {
let frekuensi = {};
let maxCount = 0;
let modus = -1;

for (let i = 0; i < arr.length; i++) {
let angka = arr[i];
if (!frekuensi[angka]) {
frekuensi[angka] = 1;
} else {
frekuensi[angka]++;
}
}

for (let i = 0; i < arr.length; i++) {
let angka = arr[i];
if (frekuensi[angka] > maxCount) {
maxCount = frekuensi[angka];
modus = angka;
}
}

if (maxCount === 1) {
return -1;
}
if (maxCount === arr.length) {
return -1;
}

return modus;
}

console.log(cariModus([10, 4, 5, 2, 4]));
console.log(cariModus([5, 10, 10, 6, 5]));
console.log(cariModus([10, 3, 1, 2, 5]));
console.log(cariModus([1, 2, 3, 3, 4, 5]));
console.log(cariModus([7, 7, 7, 7, 7]));
17 changes: 17 additions & 0 deletions quiz/part1/Part1 Soal5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function ubahHuruf(kata) {
let hasil = "";
for (let i = 0; i < kata.length; i++) {
let kode = kata.charCodeAt(i);
if (kode === 122) {
hasil += String.fromCharCode(97);
hasil += String.fromCharCode(kode + 1);
}
}
return hasil;
}

console.log(ubahHuruf("wow"));
console.log(ubahHuruf("developer"));
console.log(ubahHuruf("javascript"));
console.log(ubahHuruf("keren"));
console.log(ubahHuruf("semangat"));
23 changes: 23 additions & 0 deletions quiz/part2/Part2 Soal1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function digitPerkalianMinimum(angka) {
let minDigit = Infinity;

for (let i = 1; i <= angka; i++) {
if (angka % i === 0) {
let j = angka / i;
let gabungan = String(i) + String(j);
let panjang = gabungan.length;

if (panjang < minDigit) {
minDigit = panjang;
}
}
}

return minDigit;
}

console.log(digitPerkalianMinimum(24));
console.log(digitPerkalianMinimum(90));
console.log(digitPerkalianMinimum(20));
console.log(digitPerkalianMinimum(179));
console.log(digitPerkalianMinimum(1));
21 changes: 21 additions & 0 deletions quiz/part2/Part2 Soal2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function urutkanAbjad(str) {
let arr = str.split("");

for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - 1; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

return arr.join("");
}

console.log(urutkanAbjad("hello"));
console.log(urutkanAbjad("truncate"));
console.log(urutkanAbjad("developer"));
console.log(urutkanAbjad("software"));
console.log(urutkanAbjad("aegis"));
21 changes: 21 additions & 0 deletions quiz/part2/Part2 Soal3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function tukarBesarKecil(kalimat) {
let hasil = "";

for (let i = 0; i < kalimat.length; i++) {
let char = kalimat[i];

if (char === char.toUpperCase()) {
hasil += char.toLowerCase();
} else {
hasil += char.toUpperCase();
}
}

return hasil;
}

console.log(tukarBesarKecil("Hello World"));
console.log(tukarBesarKecil("I aM aLAY"));
console.log(tukarBesarKecil("My Name is Bond!!"));
console.log(tukarBesarKecil("IT sHOULD bE me"));
console.log(tukarBesarKecil("001-A-3-5TrdYW"));
17 changes: 17 additions & 0 deletions quiz/part2/Part2 Soal4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function checkAB(str) {
for (let i = 0; i < str.length; i++) {
if (str[i] === "a" && str[i + 4] === "b") {
return true;
}
if (str[i] === "b" && str[i + 4] === "a") {
return true;
}
}
return false;
}

console.log(checkAB("lane borrowed"));
console.log(checkAB("i am sick"));
console.log(checkAB("you are boring"));
console.log(checkAB("barbarian"));
console.log(checkAB("bacon and meat"));
27 changes: 27 additions & 0 deletions quiz/part3/Part3 Soal1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function changeMe(arr) {
if (arr.length === 0) {
console.log('""');
return;
}

const currentYear = 2023;

for (let i = 0; i < arr.length; i++) {
let person = {
firstName: arr[i][0],
lastName: arr[i][1],
gender: arr[i][2],
age: arr[i][3] ? currentYear - arr[i][3] : "Invalid Birth Year",
};

console.log(`${i + 1}. ${person.firstName} ${person.lastName}:`);
console.log(person);
}
}

changeMe([
["Christ", "Evans", "Male", 1982],
["Robert", "Downey", "Male"],
]);

changeMe([]);
45 changes: 45 additions & 0 deletions quiz/part3/Part3 Soal2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function shoppingTime(memberId, money) {
if (!memberId) {
return "Mohon maaf, toko X hanya berlaku untuk member saja";
}
if (money < 50000) {
return "Mohon maaf, uang tidak cukup";
}

let saleItems = [
["Sepatu Stacattu", 1500000],
["Baju Zoro", 500000],
["Baju H&N", 250000],
["Sweater Uniklooh", 175000],
["Casing Handphone", 50000],
];

let listPurchased = [];
let sisaUang = money;

for (let i = 0; i < saleItems.length; i++) {
let item = saleItems[i][0];
let harga = saleItems[i][1];
if (sisaUang >= harga) {
listPurchased.push(item);
sisaUang -= harga;
}
}

return {
memberId: memberId,
money: money,
listPurchased: listPurchased,
changeMoney: sisaUang,
};
}

console.log(shoppingTime("1820RzKrnWn08", 2475000));

console.log(shoppingTime("82Ku8Ma742", 170000));

console.log(shoppingTime("", 2475000));

console.log(shoppingTime("234JdhweRxa53", 15000));

console.log(shoppingTime());
64 changes: 64 additions & 0 deletions quiz/part3/Part3 Soal3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function countProfit(shoppers) {
var listBarang = [
["Sepatu Stacattu", 1500000, 10],
["Baju Zoro", 500000, 2],
["Sweater Uniklooh", 175000, 1],
];

if (shoppers.length === 0) {
return [];
}

let result = [];

for (let i = 0; i < listBarang.length; i++) {
let productName = listBarang[i][0];
let price = listBarang[i][1];
let stock = listBarang[i][2];

let obj = {
product: productName,
shoppers: [],
leftOver: stock,
totalProfit: 0,
};

for (let j = 0; j < shoppers.length; j++) {
let buyer = shoppers[j];

if (buyer.product === productName && buyer.amount <= obj.leftOver) {
obj.shoppers.push(buyer.name);
obj.leftOver -= buyer.amount;
obj.totalProfit += buyer.amount * price;
}
}

result.push(obj);
}

return result;
}

console.log(
countProfit([
{ name: "Windi", product: "Sepatu Stacattu", amount: 2 },
{ name: "Vanessa", product: "Sepatu Stacattu", amount: 3 },
{ name: "Rani", product: "Sweater Uniklooh", amount: 2 },
])
);

console.log(
countProfit([
{ name: "Windi", product: "Sepatu Stacattu", amount: 8 },
{ name: "Vanessa", product: "Sepatu Stacattu", amount: 10 },
{ name: "Rani", product: "Sweater Uniklooh", amount: 1 },
{ name: "Devi", product: "Baju Zoro", amount: 1 },
{ name: "Lisa", product: "Baju Zoro", amount: 1 },
])
);

console.log(
countProfit([{ name: "Windi", product: "Sepatu Naiki", amount: 5 }])
);

console.log(countProfit([]));
45 changes: 45 additions & 0 deletions quiz/ujian/Ujian Soal 3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function highestScore(students) {
let result = {};

for (let i = 0; i < students.length; i++) {
let student = students[i];
let kelas = student.class;

if (!result[kelas]) {
result[kelas] = {
name: student.name,
score: student.score,
};
} else {
if (student.score > result[kelas].score) {
result[kelas] = {
name: student.name,
score: student.score,
};
}
}
}

return result;
}

console.log(
highestScore([
{ name: "Dimitri", score: 90, class: "foxes" },
{ name: "Alexei", score: 85, class: "wolves" },
{ name: "Sergei", score: 74, class: "foxes" },
{ name: "Anastasia", score: 78, class: "wolves" },
])
);

console.log(
highestScore([
{ name: "Alexander", score: 100, class: "foxes" },
{ name: "Alisa", score: 76, class: "wolves" },
{ name: "Vladimir", score: 92, class: "foxes" },
{ name: "Albert", score: 71, class: "wolves" },
{ name: "Viktor", score: 80, class: "tigers" },
])
);

console.log(highestScore([]));
Loading