-
Notifications
You must be signed in to change notification settings - Fork 121
Tugas Week 3 #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ikbal129
wants to merge
4
commits into
RPN-Phase-0:main
Choose a base branch
from
ikbal129:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Tugas Week 3 #74
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //cek apakah angka yang dikirim adalah angka prima atau bukan? | ||
| //cek google bagi yang ga tau apa itu angka prima | ||
| function angkaPrima(angka) { | ||
| if (angka <= 2) { | ||
| return false; | ||
| } | ||
|
|
||
| let cekAngka = Math.floor(Math.sqrt(angka)); | ||
|
|
||
| for (let i = 2; i <= cekAngka; i++) { | ||
| let hasil = angka % i; | ||
| if (hasil == 0) { | ||
| return false; | ||
| } | ||
| } | ||
| return 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 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| //cari faktor persekutuan terbesar | ||
| function fpb(angka1, angka2) { | ||
| //faktor angka1 | ||
| let faktor1 = []; | ||
| for (let i = 0; i < angka1; i++) { | ||
| if (angka1 % i == 0) { | ||
| faktor1.push(i); | ||
| } | ||
| } | ||
| //faktor angka2 | ||
| let faktor2 = []; | ||
| for (let i = 0; i < angka2; i++) { | ||
| if (angka2 % i == 0) { | ||
| faktor2.push(i) | ||
| } | ||
| } | ||
|
|
||
| let sama = faktor1.filter(num => faktor2.includes(num)); | ||
| return sama[sama.length - 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 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| function cariMedian(arr) { | ||
| //genap or ganjil | ||
| let hasil; | ||
| if (arr.length % 2 == 0) { | ||
| //urutkan | ||
| let data = arr.sort((a, b) => { | ||
| return a - b | ||
| }); | ||
| let num1 = data.length / 2; | ||
| let num2 = (data.length / 2) - 1; | ||
| hasil = (arr[num1] + arr[num2]) / 2; | ||
| return hasil; | ||
| } else { | ||
| hasil = (arr.length - 1) / 2; | ||
| return arr[hasil]; | ||
| } | ||
| } | ||
|
|
||
| // 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])); // 7 | ||
| console.log(cariMedian([1, 3, 3])); // 3 | ||
| console.log(cariMedian([7, 7, 8, 8])); // 7.5 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| 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) { | ||
| let angka = {} | ||
|
|
||
| for (let i = 0; i < arr.length; i++) { | ||
|
|
||
| if (angka[arr[i]]) { | ||
| angka[arr[i]]++; | ||
| } else { | ||
| angka[arr[i]] = 1; | ||
| } | ||
| } | ||
|
|
||
| let angkaArr = Object.entries(angka); | ||
| if (angkaArr.length == 1) { | ||
| return -1; | ||
| } | ||
|
|
||
| let hasil = []; | ||
| for (let i = 0; i < angkaArr.length; i++) { | ||
| hasil.push(angkaArr[i][1]); | ||
| } | ||
|
|
||
| let hasilSort = hasil.sort((a,b) => { | ||
| return a - b; | ||
| }) | ||
|
|
||
| let semuaSatu = angkaArr.every(([_, count]) => count === 1); | ||
| if (semuaSatu) return -1; | ||
|
|
||
| for (let j = 0; j < hasil.length; j++) { | ||
| if (angkaArr[j][1] == hasilSort[hasilSort.length - 1]) { | ||
| return angkaArr[j][0] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 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 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| //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) { | ||
| let strCode = []; | ||
| //ubah jadi angka / ASCII | ||
| for (let i = 0; i < kata.length; i++) { | ||
| let code = kata[i].charCodeAt(); | ||
| code++; | ||
| strCode.push(code); | ||
| } | ||
| // ubah balik jadi huruf; | ||
| let kalimat = []; | ||
| let huruf; | ||
| for (let i = 0; i < strCode.length; i++) { | ||
| if (strCode[i] == 123) { | ||
| huruf = "a"; | ||
| } else { | ||
| huruf = String.fromCharCode(strCode[i]); | ||
| } | ||
|
|
||
| kalimat.push(huruf); | ||
| } | ||
| return kalimat.join(""); | ||
| } | ||
|
|
||
| // 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 | ||
| console.log(ubahHuruf('zebra')); | ||
| console.log(ubahHuruf('anzay')); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| 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) { | ||
| let minDigit = Infinity;//untuk mebandingkan dengan angka terbesar | ||
|
|
||
| for (let i = 1; i <= angka; i++) { | ||
| //looping cari faktor | ||
| if (angka % i == 0) { | ||
| let pasanganKali = angka/i;//cari pasangan faktor | ||
| let digit = `${i}${pasanganKali}`;//gabung dengan menjadikan string | ||
|
|
||
| if (digit.length < minDigit) { | ||
| minDigit = digit.length;//jika lebih kecil maka update minDigit | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return minDigit; | ||
| } | ||
|
|
||
| // 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 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| //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) { | ||
| const arr = [...str]; | ||
| let code = arr.map((e) => { | ||
| return e.charCodeAt(); | ||
| }) | ||
|
|
||
| const n = code.length; | ||
| let swapped; | ||
|
|
||
| for (let i = 0; i < n; i++) { | ||
| swapped = false; | ||
|
|
||
| for (let j = 0; j < n - i - 1; j++) { | ||
| if (code[j] > code[j + 1]) { | ||
| let temp = code[j]; | ||
| code[j] = code[j + 1]; | ||
| code[j + 1] = temp; | ||
| swapped = true; | ||
| } | ||
| } | ||
|
|
||
| if (!swapped) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| let hasil = code.map((e) => { | ||
| return String.fromCharCode(e); | ||
| }) | ||
| return hasil.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' |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //TIPS: gunakan method toUpperCase() dan toLowerCase() | ||
| function tukarBesarKecil(kalimat) { | ||
| let arr = [...kalimat]; | ||
| let hasil = []; | ||
|
|
||
| for (let i = 0; i < arr.length; i++) { | ||
| if (arr[i] == arr[i].toUpperCase()) { | ||
| hasil.push(arr[i].toLowerCase()); | ||
| } else { | ||
| hasil.push(arr[i].toUpperCase()); | ||
| } | ||
| } | ||
|
|
||
| return hasil.join(""); | ||
| } | ||
|
|
||
| // 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" |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bisa sekali loop, cukup cek a dan b yang berjarak 4.. Bisa pakai if num[i] === a && num[i+4] === b dan sebaliknya |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| 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) { | ||
| let inA = []; | ||
| let inB = []; | ||
|
|
||
| for (let i = 0; i < num.length; i++) { | ||
| if (num[i] == "a") { | ||
| inA.push(i); | ||
| } else if (num[i] == "b") { | ||
| inB.push(i); | ||
| } | ||
| } | ||
|
|
||
| if (inA.length == 0 || inB.length == 0) { | ||
| return false; | ||
| } | ||
|
|
||
| for (let i of inA) { | ||
| for (let j of inB) { | ||
| let jarak = Math.abs(i - j); | ||
| let hasil; | ||
| if (jarak === 4) { | ||
| 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 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| function changeMe(arr) { | ||
| for (let i = 0; i < arr.length; i++) { | ||
| const fullName = arr[i][0] + " " + arr[i][1]; | ||
| let data = { [fullName]: {} }; // pakai computed property | ||
|
|
||
| const first = arr[i][0]; | ||
| const last = arr[i][1]; | ||
| const gen = arr[i][2]; | ||
|
|
||
| data[fullName].firstName = first; | ||
| data[fullName].lastName = last; | ||
| data[fullName].gender = gen; | ||
|
|
||
| if (arr[i][3] == undefined) { | ||
| data[fullName].age = "Invalid Birth Year"; | ||
| } else { | ||
| const age = 2025 - arr[i][3]; | ||
| data[fullName].age = age; | ||
| } | ||
|
|
||
| console.log(data); | ||
| } | ||
| } | ||
|
|
||
| // 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([]); // "" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ga perlu diubah ke array juga bisa sebenernya, but nice approach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Siap sir, makasih masukannya