Last active
April 11, 2022 13:16
-
-
Save mcanam/b2ab617aa70f30bd440d314697c37bb4 to your computer and use it in GitHub Desktop.
logic test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Alternate each case of each of string given | |
alternateCase("abc") => "ABC" | |
alternateCase("ABC") => "abc" | |
alternateCase("Hello World") => "hELLO wORLD" | |
*/ | |
function alternateCase(input) { | |
// pisahkan setiap karakter pada input | |
// menjadi array dan simpan kedalam variable chars | |
const chars = input.split(""); | |
let output = ""; | |
chars.forEach(char => { | |
// check apakah char uppercase? | |
// jika ya maka ubah jadi lowercase | |
// begitupun sebaliknya. | |
if (char == char.toUpperCase()) { | |
output += char.toLowerCase(); | |
} else { | |
output += char.toUpperCase(); | |
} | |
}); | |
// selesai. | |
return output; | |
} | |
console.log(alternateCase("abc")); // => "ABC" | |
console.log(alternateCase("ABC")); // => "abc" | |
console.log(alternateCase("Hello World")); // => "hELLO wORLD" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Description: | |
Create a function that takes one positive three digit integer and rearranges | |
its digits to get the maximum possible number. Assume that the argument is an integer. | |
Returm null if the argument is invalid. | |
maxRedigit(123) --> 321 | |
maxRedigit(231) --> 321 | |
maxRedigit(321) --> 321 | |
maxRedigit(-1) --> null | |
maxRedigit(0) --> null | |
maxRedigit(99) --> null | |
maxRedigit(1000) --> null | |
*/ | |
function maxRedigit(input) { | |
// jika tipe data input bukan number atau | |
// jika input lebih kecil dari 100 atau | |
// jika input lebih besar dari 999 | |
// return null | |
if (typeof input != "number" || (input < 100 || input > 999)) { | |
return null; | |
} | |
// ubah input menjadi string | |
// lalu pisahkan setiap karakter | |
// menjadi array dan simpan kedalam variable chars | |
let chars = input.toString().split(""); | |
// lalu sortir setiap element | |
// pada array chars dengan urutan menaik | |
chars = chars.sort((a, b) => b - a); | |
// gabungkan kembali setiap karakter | |
// lalu konversi menjadi integer / number | |
const output = parseInt(chars.join("")); | |
// kembalikan angka yang sudah disusun ulang. | |
// selesai. | |
return output; | |
} | |
console.log(maxRedigit(123)); // --> 321 | |
console.log(maxRedigit(231)); // --> 321 | |
console.log(maxRedigit(321)); // --> 321 | |
console.log(maxRedigit(-1)); // --> null | |
console.log(maxRedigit(0)); // --> null | |
console.log(maxRedigit(99)); // --> null | |
console.log(maxRedigit(1000)); // --> null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given an array/list [] of integers , Construct a product array Of same size Such That prod[i] | |
is equal to The Product of all the elements of Arr[] except Arr[i]. | |
productArray([12,20]) => [20,12] | |
productArray([12,20]) => [20,12] | |
productArray([3,27,4,2]) => [216,24,162,324] | |
productArray([13,10,5,2,9]) => [900,1170,2340,5850,1300] | |
productArray([16,17,4,3,5,2]) => [2040,1920,8160,10880,6528,16320] | |
*/ | |
function productArray(input) { | |
// jika input hanya berisi 1 element | |
// hentikan program dan kembalikan | |
// array tersebut | |
if (input.length == 1) return input; | |
const output = input.map((e1, i1) => { | |
// variable untuk menampung nilai yang | |
// sudah dikalikan | |
let tmp = null; | |
input.forEach((e2, i2) => { | |
// jika index2 sama dengan index1 | |
// lanjut ke index berikutnya | |
if (i2 == i1) return; | |
// jika tmp tidak null maka | |
// kalikan nilai tmp dengan | |
// element array saat ini | |
if (tmp != null) tmp *= e2; | |
// jika null | |
// set value tmp dengan nilai | |
// element array saat ini. | |
else tmp = e2; | |
}); | |
return tmp; | |
}); | |
// selesai | |
return output; | |
} | |
console.log(productArray([12,20])); // => [20,12] | |
console.log(productArray([12,20])); // => [20,12] | |
console.log(productArray([3,27,4,2])); // => [216,24,162,324] | |
console.log(productArray([13,10,5,2,9])); // => [900,1170,2340,5850,1300] | |
console.log(productArray([16,17,4,3,5,2])); // => [2040,1920,8160,10880,6528,16320] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Create solution function that accept 1 parameter that be will multiple number 3 and 5 | |
while each result of that multiplication is still lower than parameter inputed | |
solution (10) // => 23 = 3 + 5 + 6 + 9 | |
solution (20) // => 78 = 3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 | |
*/ | |
function solution(input) { | |
let output = 0; | |
for (let i = 1; i < input; i++) { | |
// jika i habis dibagi atau | |
// kelipatan dari 3 / 5 | |
// tambahkan nilai i dengan nilai output | |
if (i % 3 == 0 || i % 5 == 0) { | |
output += i; | |
} | |
} | |
// selesai. | |
return output; | |
} | |
// console.assert(solution(10) == 23); | |
// console.assert(solution(20) == 78); | |
console.log(solution(10)); // => 23 = 3 + 5 + 6 + 9 | |
console.log(solution(20)); // => 78 = 3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment