From bdb178079bbe67d65e02640b907f8aeba411ccb3 Mon Sep 17 00:00:00 2001 From: Okkura Date: Sat, 7 Feb 2026 23:13:17 +0700 Subject: [PATCH] first commit --- Loop-Array/countingSheep.js | 52 ++++++++++---- Loop-Array/findHighest.js | 17 +++-- Loop-Array/findNeedle.js | 87 ++++++++++++++++++++++-- Loop-Array/findSmallest.js | 17 +++-- Loop-Array/positifSum.js | 16 +++-- Loop-Array/squareSum.js | 12 +++- Loop-Number/centuryYear.js | 4 +- Loop-Number/summation.js | 10 ++- Loop-String/abbrevName.js | 12 +++- Loop-String/noSpace.js | 15 +++- Loop-String/removeChar.js | 26 ++++--- Loop-String/stringRepeat.js | 12 +++- Pattern-Printing/diamond.js | 15 +++- Pattern-Printing/papanCatur.js | 22 +++++- Pattern-Printing/patternX.js | 26 ++++--- Pattern-Printing/persegi.js | 10 ++- Pattern-Printing/persegiBolong.js | 18 ++++- Pattern-Printing/piramid.js | 14 +++- Pattern-Printing/piramid2.js | 18 ++++- Pattern-Printing/piramidTerbalik.js | 16 ++++- Pattern-Printing/segitigaSiku.js | 10 ++- Pattern-Printing/segitigaSikuTerbalik.js | 10 ++- 22 files changed, 354 insertions(+), 85 deletions(-) diff --git a/Loop-Array/countingSheep.js b/Loop-Array/countingSheep.js index e0ee29a..33f6d1e 100644 --- a/Loop-Array/countingSheep.js +++ b/Loop-Array/countingSheep.js @@ -4,18 +4,44 @@ function countSheeps(sheep) { // TODO + let result = 0; + for (i = 0; i < sheep.length; i++) { + sheep[i] ? (result += 1) : null; + } + + return result; } -console.log(countSheeps([])) //0 -console.log(countSheeps([undefined])) //0 -console.log(countSheeps([null])) //0 -console.log(countSheeps([false])) //0 -console.log(countSheeps([true])) //1 -console.log(countSheeps( - [true,true,true,false, - true,true,true,true, - true,false,true,false, - true,false,false,true, - true,true,true,true, - false,false,true,true -])) //17 \ No newline at end of file +console.log(countSheeps([])); //0 +console.log(countSheeps([undefined])); //0 +console.log(countSheeps([null])); //0 +console.log(countSheeps([false])); //0 +console.log(countSheeps([true])); //1 +console.log( + countSheeps([ + true, + true, + true, + false, + true, + true, + true, + true, + true, + false, + true, + false, + true, + false, + false, + true, + true, + true, + true, + true, + false, + false, + true, + true, + ]), +); //17 diff --git a/Loop-Array/findHighest.js b/Loop-Array/findHighest.js index ffb59a5..3b98450 100644 --- a/Loop-Array/findHighest.js +++ b/Loop-Array/findHighest.js @@ -4,11 +4,16 @@ function findHighestInt(arr) { //your code here - + let highest = arr[0]; + for (i = 0; i < arr.length; i++) { + arr[i] > highest ? (highest = arr[i]) : null; + } + + return highest; } -console.log(findHighestInt([78,56,232,12,8])); //8 -console.log(findHighestInt([78,56,232,12,18])); //12 -console.log(findHighestInt([78,56,232,412,228])); //56 -console.log(findHighestInt([78,56,232,12,0])); //0 -console.log(findHighestInt([1,56,232,12,8])); //1 \ No newline at end of file +console.log(findHighestInt([78, 56, 232, 12, 8])); //8 +console.log(findHighestInt([78, 56, 232, 12, 18])); //12 +console.log(findHighestInt([78, 56, 232, 412, 228])); //56 +console.log(findHighestInt([78, 56, 232, 12, 0])); //0 +console.log(findHighestInt([1, 56, 232, 12, 8])); //1 diff --git a/Loop-Array/findNeedle.js b/Loop-Array/findNeedle.js index d4c9660..7e172d4 100644 --- a/Loop-Array/findNeedle.js +++ b/Loop-Array/findNeedle.js @@ -4,14 +4,91 @@ function findNeedle(haystack) { // your code here + for (i = 0; i < haystack.length; i++) { + if (haystack[i] === "needle") { + return `found the needle at position ${i}`; + } + } } -let haystack_1 = ['3', '123124234', undefined, 'needle', 'world', 'hay', 2, '3', true, false]; -let haystack_2 = ['283497238987234', 'a dog', 'a cat', 'some random junk', 'a piece of hay', 'needle', 'something somebody lost a while ago']; -let haystack_3 = [1,2,3,4,5,6,7,8,8,7,5,4,3,4,5,6,67,5,5,3,3,4,2,34,234,23,4,234,324,324,'needle',1,2,3,4,5,5,6,5,4,32,3,45,54]; -let haystack_4 = ["hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk"]; +let haystack_1 = [ + "3", + "123124234", + undefined, + "needle", + "world", + "hay", + 2, + "3", + true, + false, +]; +let haystack_2 = [ + "283497238987234", + "a dog", + "a cat", + "some random junk", + "a piece of hay", + "needle", + "something somebody lost a while ago", +]; +let haystack_3 = [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 8, + 7, + 5, + 4, + 3, + 4, + 5, + 6, + 67, + 5, + 5, + 3, + 3, + 4, + 2, + 34, + 234, + 23, + 4, + 234, + 324, + 324, + "needle", + 1, + 2, + 3, + 4, + 5, + 5, + 6, + 5, + 4, + 32, + 3, + 45, + 54, +]; +let haystack_4 = [ + "hay", + "junk", + "hay", + "hay", + "moreJunk", + "needle", + "randomJunk", +]; console.log(findNeedle(haystack_1)); //found the needle at position 3 console.log(findNeedle(haystack_2)); //found the needle at position 5 console.log(findNeedle(haystack_3)); //found the needle at position 30 -console.log(findNeedle(haystack_4)); //found the needle at position 5 \ No newline at end of file +console.log(findNeedle(haystack_4)); //found the needle at position 5 diff --git a/Loop-Array/findSmallest.js b/Loop-Array/findSmallest.js index ad42028..039e8e3 100644 --- a/Loop-Array/findSmallest.js +++ b/Loop-Array/findSmallest.js @@ -4,11 +4,16 @@ function findSmallestInt(arr) { //your code here - + let smallest = arr[0]; + for (i = 0; i < arr.length; i++) { + arr[i] < smallest ? (smallest = arr[i]) : null; + } + + return smallest; } -console.log(findSmallestInt([78,56,232,12,8])); //8 -console.log(findSmallestInt([78,56,232,12,18])); //12 -console.log(findSmallestInt([78,56,232,412,228])); //56 -console.log(findSmallestInt([78,56,232,12,0])); //0 -console.log(findSmallestInt([1,56,232,12,8])); //1 \ No newline at end of file +console.log(findSmallestInt([78, 56, 232, 12, 8])); //8 +console.log(findSmallestInt([78, 56, 232, 12, 18])); //12 +console.log(findSmallestInt([78, 56, 232, 412, 228])); //56 +console.log(findSmallestInt([78, 56, 232, 12, 0])); //0 +console.log(findSmallestInt([1, 56, 232, 12, 8])); //1 diff --git a/Loop-Array/positifSum.js b/Loop-Array/positifSum.js index 6483e00..59b0a2a 100644 --- a/Loop-Array/positifSum.js +++ b/Loop-Array/positifSum.js @@ -4,9 +4,17 @@ function positiveSum(arr) { //code here + let result = 0; + for (i = 0; i < arr.length; i++) { + if (arr[i] > 0) { + result += arr[i]; + } + } + + return result; } -console.log(positiveSum([1,2,3,4,5])) //15 -console.log(positiveSum([1,-2,3,4,5])) //13 -console.log(positiveSum([])) //13 -console.log(positiveSum([-1,2,3,4,-5])) //9 \ No newline at end of file +console.log(positiveSum([1, 2, 3, 4, 5])); //15 +console.log(positiveSum([1, -2, 3, 4, 5])); //13 +console.log(positiveSum([])); //13 +console.log(positiveSum([-1, 2, 3, 4, -5])); //9 diff --git a/Loop-Array/squareSum.js b/Loop-Array/squareSum.js index 9d07259..60e1051 100644 --- a/Loop-Array/squareSum.js +++ b/Loop-Array/squareSum.js @@ -2,10 +2,16 @@ //tambahkan dengan pangkat 2 (n**2) //source: https://www.codewars.com/kata/515e271a311df0350d00000f -function squareSum(numbers){ +function squareSum(numbers) { //code here + let result = 0; + for (i = 0; i < numbers.length; i++) { + result += numbers[i] ** 2; + } + + return result; } -console.log(squareSum([1,2])); //5 +console.log(squareSum([1, 2])); //5 console.log(squareSum([0, 3, 4, 5])); //50 -console.log(squareSum([])); //0 \ No newline at end of file +console.log(squareSum([])); //0 diff --git a/Loop-Number/centuryYear.js b/Loop-Number/centuryYear.js index 296af71..c89a081 100644 --- a/Loop-Number/centuryYear.js +++ b/Loop-Number/centuryYear.js @@ -6,11 +6,11 @@ function century(year) { // Finish this :) - return; + return Math.ceil(year / 100); } console.log(century(1705)); //18 console.log(century(1900)); //19 console.log(century(1601)); //17 console.log(century(2000)); //20 -console.log(century(89)); //1 \ No newline at end of file +console.log(century(89)); //1 diff --git a/Loop-Number/summation.js b/Loop-Number/summation.js index 327ab74..b081cce 100644 --- a/Loop-Number/summation.js +++ b/Loop-Number/summation.js @@ -3,10 +3,16 @@ //8 -> 36 (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8) //source: https://www.codewars.com/kata/55d24f55d7dd296eb9000030 -function summation (num) { +function summation(num) { // Code here + let result = 0; + for (i = 1; i <= num; i++) { + result += i; + } + + return result; } console.log(summation(1)); //1 console.log(summation(2)); //3 -console.log(summation(8)); //36 \ No newline at end of file +console.log(summation(8)); //36 diff --git a/Loop-String/abbrevName.js b/Loop-String/abbrevName.js index 48124c2..244d8a1 100644 --- a/Loop-String/abbrevName.js +++ b/Loop-String/abbrevName.js @@ -3,12 +3,20 @@ // patrick feeney => P.F //source: https://www.codewars.com/kata/57eadb7ecd143f4c9c0000a3 -function abbrevName(name){ +function abbrevName(name) { //code here + let splited = name.split(" "); + let result = []; + + for (i = 0; i < splited.length; i++) { + result.push(splited[i][0]); + } + + return result.join("."); } console.log(abbrevName("Sam Harris")); //S.H console.log(abbrevName("Patrick Feenan")); //P.F console.log(abbrevName("Evan Cole")); //E.C console.log(abbrevName("P Favuzzi")); //P.F -console.log(abbrevName("David Mendieta")); //D.M \ No newline at end of file +console.log(abbrevName("David Mendieta")); //D.M diff --git a/Loop-String/noSpace.js b/Loop-String/noSpace.js index e62ba23..fe28287 100644 --- a/Loop-String/noSpace.js +++ b/Loop-String/noSpace.js @@ -2,10 +2,19 @@ //hilangkan spasinya saja //source: https://www.codewars.com/kata/57eae20f5500ad98e50002c5 -function noSpace(x){ +function noSpace(x) { //code here + let splited = x.split(""); + let result = []; + for (i = 0; i < splited.length; i++) { + if (splited[i] !== " ") { + result.push(splited[i]); + } + } + + return result.join(""); } -console.log(noSpace("8 j 8 mBliB8g imjB8B8 jl B")) //8j8mBliB8gimjB8B8jlB -console.log(noSpace("8 8 Bi fk8h B 8 BB8B B B B888 c hl8 BhB fd")) //88Bifk8hB8BB8BBBB888chl8BhBfd \ No newline at end of file +console.log(noSpace("8 j 8 mBliB8g imjB8B8 jl B")); //8j8mBliB8gimjB8B8jlB +console.log(noSpace("8 8 Bi fk8h B 8 BB8B B B B888 c hl8 BhB fd")); //88Bifk8hB8BB8BBBB888chl8BhBfd diff --git a/Loop-String/removeChar.js b/Loop-String/removeChar.js index 2c09bed..d3f41d6 100644 --- a/Loop-String/removeChar.js +++ b/Loop-String/removeChar.js @@ -2,13 +2,21 @@ //hapus huruf pertama dan terakhir //source: https://www.codewars.com/kata/56bc28ad5bdaeb48760009b0 -function removeChar(str){ - -}; +function removeChar(str) { + let result = ""; + for (let i = 0; i < str.length; i++) { + if (i === 0 || i === str.length - 1) { + continue; + } + result += str[i]; + } -console.log(removeChar('eloquent')); //loquen -console.log(removeChar('country')); //ountr -console.log(removeChar('person')); //ountr -console.log(removeChar('place')); //lac -console.log(removeChar('ab')) // -console.log(removeChar('ooopsss')); //oopss \ No newline at end of file + return result; +} + +console.log(removeChar("eloquent")); //loquen +console.log(removeChar("country")); //ountr +console.log(removeChar("person")); //ountr +console.log(removeChar("place")); //lac +console.log(removeChar("ab")); // +console.log(removeChar("ooopsss")); //oopss diff --git a/Loop-String/stringRepeat.js b/Loop-String/stringRepeat.js index ca7175f..27ca729 100644 --- a/Loop-String/stringRepeat.js +++ b/Loop-String/stringRepeat.js @@ -2,15 +2,21 @@ //hanya mengulang string sebanyak n kali //source: https://www.codewars.com/kata/57a0e5c372292dd76d000d7e -function repeatStr (n, s) { +function repeatStr(n, s) { //code here + let result = ""; + for (i = 1; i <= n; i++) { + result += s; + } + + return result; } console.log(repeatStr(3, "*")); //*** console.log(repeatStr(5, "#")); //##### -console.log(repeatStr(2, "ha ")); //ha ha +console.log(repeatStr(2, "ha ")); //ha ha console.log(repeatStr(0, "")); // console.log(repeatStr(0, "I")); // console.log(repeatStr(5, "")); // console.log(repeatStr(6, "I")); //IIIIII -console.log(repeatStr(5, "Hello")); //HelloHelloHelloHelloHello \ No newline at end of file +console.log(repeatStr(5, "Hello")); //HelloHelloHelloHelloHello diff --git a/Pattern-Printing/diamond.js b/Pattern-Printing/diamond.js index a813fb7..57a398c 100644 --- a/Pattern-Printing/diamond.js +++ b/Pattern-Printing/diamond.js @@ -1,6 +1,17 @@ function berlian(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = 1; i <= num; i++) { + pattern += " ".repeat(num - i); + pattern += "*".repeat(2 * i - 1); + pattern += "\n"; + } + + for (let i = num - 1; i >= 1; i--) { + pattern += " ".repeat(num - i); + pattern += "*".repeat(2 * i - 1); + pattern += "\n"; + } return pattern; } @@ -17,4 +28,4 @@ expected result ***** *** * -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/papanCatur.js b/Pattern-Printing/papanCatur.js index c90644e..49761e8 100644 --- a/Pattern-Printing/papanCatur.js +++ b/Pattern-Printing/papanCatur.js @@ -2,8 +2,26 @@ // Petunjuk: Gunakan logika 'ganjil-genap' dari // penjumlahan indeks baris (i) dan kolom (j). function papanCatur(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = 1; i <= num; i++) { + for (let j = 1; j <= num; j++) { + if (i % 2 === 0) { + if (j % 2 === 0) { + pattern += "#"; + } else { + pattern += " "; + } + } else { + if (j % 2 === 0) { + pattern += " "; + } else { + pattern += "#"; + } + } + } + pattern += "\n"; + } return pattern; } @@ -16,4 +34,4 @@ expected result # # # # # # # # -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/patternX.js b/Pattern-Printing/patternX.js index b191574..43f47ba 100644 --- a/Pattern-Printing/patternX.js +++ b/Pattern-Printing/patternX.js @@ -3,19 +3,29 @@ // berada di diagonal utama (i === j) ATAU // diagonal terbalik (i + j === num - 1). function polaX(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = 0; i < num; i++) { + for (let j = 0; j < num; j++) { + if (i === j || i + j === num - 1) { + pattern += "*"; + } else { + pattern += " "; + } + } + pattern += "\n"; + } return pattern; } // use case -console.log('\n--- Soal 11: Pola X ---'); +console.log("\n--- Soal 11: Pola X ---"); console.log(polaX(5)); /* expected result -* * - * * - * - * * -* * -*/ \ No newline at end of file +* * i = 1 dan j = 4 + * * i = 2 dan j = 3 + * i = 3 dan j = 2 + * * i = 4 dan j = 1 +* * i = 5 dan j = 0 +*/ diff --git a/Pattern-Printing/persegi.js b/Pattern-Printing/persegi.js index 1c28f6d..3aaa229 100644 --- a/Pattern-Printing/persegi.js +++ b/Pattern-Printing/persegi.js @@ -1,7 +1,13 @@ // Wajib menggunakan nested loop function persegi(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = 1; i <= num; i++) { + for (let j = 1; j <= num; j++) { + pattern += "*"; + } + pattern += "\n"; + } return pattern; } @@ -13,4 +19,4 @@ expected result **** **** **** -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/persegiBolong.js b/Pattern-Printing/persegiBolong.js index 398fc02..0cd772a 100644 --- a/Pattern-Printing/persegiBolong.js +++ b/Pattern-Printing/persegiBolong.js @@ -1,7 +1,21 @@ // Wajib menggunakan nested loop dan conditional (if/else) di dalamnya. function persegiBolong(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = 0; i < num; i++) { + for (let j = 0; j < num; j++) { + if (i === 0 || i === num - 1) { + pattern += "*"; + } else { + if (j === 0 || j === num - 1) { + pattern += "*"; + } else { + pattern += " "; + } + } + } + pattern += "\n"; + } return pattern; } @@ -14,4 +28,4 @@ expected result * * * * ***** -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/piramid.js b/Pattern-Printing/piramid.js index afa2b5d..a89ff61 100644 --- a/Pattern-Printing/piramid.js +++ b/Pattern-Printing/piramid.js @@ -1,8 +1,18 @@ // Wajib menggunakan nested loop. // Petunjuk: Anda mungkin perlu 2 loop di dalam 1 loop (satu untuk spasi, satu untuk bintang). function piramida(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = 1; i <= num; i++) { + for (let j = i; j < num; j++) { + pattern += " "; + } + + for (let k = 1; k <= i * 2 - 1; k++) { + pattern += "*"; + } + pattern += "\n"; + } return pattern; } @@ -15,4 +25,4 @@ expected result ***** ******* ********* -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/piramid2.js b/Pattern-Printing/piramid2.js index 14e316d..551d794 100644 --- a/Pattern-Printing/piramid2.js +++ b/Pattern-Printing/piramid2.js @@ -1,8 +1,22 @@ // Wajib menggunakan nested loop. // Petunjuk: Anda mungkin perlu 2 loop di dalam 1 loop (satu untuk spasi, satu untuk bintang). function piramida2(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = 1; i <= num; i++) { + for (let j = i; j < num; j++) { + pattern += " "; + } + + for (let k = 1; k <= i * 2 - 1; k++) { + if (k % 2 === 0) { + pattern += " "; + } else { + pattern += "*"; + } + } + pattern += "\n"; + } return pattern; } @@ -15,4 +29,4 @@ expected result * * * * * * * * * * * * -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/piramidTerbalik.js b/Pattern-Printing/piramidTerbalik.js index 8c5677e..7be3d08 100644 --- a/Pattern-Printing/piramidTerbalik.js +++ b/Pattern-Printing/piramidTerbalik.js @@ -1,6 +1,16 @@ function piramidaTerbalik(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = 1; i <= num; i++) { + for (let j = 1; j < i; j++) { + pattern += " "; + } + + for (let k = 1; k <= num * 2 - (2 * i - 1); k++) { + pattern += "*"; + } + pattern += "\n"; + } return pattern; } @@ -8,9 +18,9 @@ function piramidaTerbalik(num) { console.log(piramidaTerbalik(5)); /* expected result -********* +********* ******* ***** *** * -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/segitigaSiku.js b/Pattern-Printing/segitigaSiku.js index c5cce1e..365eca8 100644 --- a/Pattern-Printing/segitigaSiku.js +++ b/Pattern-Printing/segitigaSiku.js @@ -1,7 +1,13 @@ // Pada quiz ini wajib menggunakan nested loop function segitigaSiku(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = 1; i <= num; i++) { + for (let j = 1; j <= i; j++) { + pattern += "*"; + } + pattern += "\n"; + } return pattern; } @@ -14,4 +20,4 @@ expected result *** **** ***** -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/segitigaSikuTerbalik.js b/Pattern-Printing/segitigaSikuTerbalik.js index 287c2dd..8e883a1 100644 --- a/Pattern-Printing/segitigaSikuTerbalik.js +++ b/Pattern-Printing/segitigaSikuTerbalik.js @@ -1,7 +1,13 @@ // Wajib menggunakan nested loop function segitigaTerbalik(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = 0; i < num; i++) { + for (let j = 1; j <= num - i; j++) { + pattern += "*"; + } + pattern += "\n"; + } return pattern; } @@ -14,4 +20,4 @@ expected result *** ** * -*/ \ No newline at end of file +*/