From c7ac62eb52f9f636eea22ab9bd0e998077f71a39 Mon Sep 17 00:00:00 2001 From: zxcvbnm0091 Date: Tue, 30 Dec 2025 09:57:25 +0700 Subject: [PATCH 1/3] initial commit --- Loop-Array/countingSheep.js | 51 +++++++++++---- Loop-Array/findHighest.js | 17 +++-- Loop-Array/findNeedle.js | 86 ++++++++++++++++++++++++-- Loop-Array/findSmallest.js | 16 +++-- Loop-Array/positifSum.js | 13 ++-- Loop-Array/squareSum.js | 11 +++- Loop-Number/centuryYear.js | 12 +++- Loop-Number/summation.js | 9 ++- Loop-String/abbrevName.js | 11 +++- Loop-String/noSpace.js | 12 ++-- Loop-String/removeChar.js | 22 ++++--- Loop-String/stringRepeat.js | 11 +++- Pattern-Printing/diamond.js | 15 ++++- Pattern-Printing/papanCatur.js | 13 ++-- Pattern-Printing/tempCodeRunnerFile.js | 15 +++++ 15 files changed, 246 insertions(+), 68 deletions(-) create mode 100644 Pattern-Printing/tempCodeRunnerFile.js diff --git a/Loop-Array/countingSheep.js b/Loop-Array/countingSheep.js index e0ee29a..15480d3 100644 --- a/Loop-Array/countingSheep.js +++ b/Loop-Array/countingSheep.js @@ -4,18 +4,43 @@ function countSheeps(sheep) { // TODO + let sum = 0; + for (let i = 0; i < sheep.length; i++) { + if (sheep[i]) sum += 1; + } + return sum; } -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..c55da40 100644 --- a/Loop-Array/findHighest.js +++ b/Loop-Array/findHighest.js @@ -4,11 +4,16 @@ function findHighestInt(arr) { //your code here - + let highest = 0; + for (let i = 0; i < arr.length; i++) { + highest = arr[i] > highest ? arr[i] : highest; + } + + 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..277784c 100644 --- a/Loop-Array/findNeedle.js +++ b/Loop-Array/findNeedle.js @@ -4,14 +4,90 @@ function findNeedle(haystack) { // your code here + for (let i = 0; i < haystack.length; i++) { + if (haystack[i] === "needle") return `found the needle at position ${i}`; + } + return "Needle not found"; } -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..d8e5547 100644 --- a/Loop-Array/findSmallest.js +++ b/Loop-Array/findSmallest.js @@ -4,11 +4,15 @@ function findSmallestInt(arr) { //your code here - + let smallest = Infinity; + for (let i = 0; i < arr.length; i++) { + smallest = arr[i] < smallest ? arr[i] : smallest; + } + 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..8344cf9 100644 --- a/Loop-Array/positifSum.js +++ b/Loop-Array/positifSum.js @@ -4,9 +4,14 @@ function positiveSum(arr) { //code here + let sum = 0; + for (let i = 0; i < arr.length; i++) { + sum += arr[i] > 0 ? arr[i] : 0; + } + return sum; } -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..64f57a1 100644 --- a/Loop-Array/squareSum.js +++ b/Loop-Array/squareSum.js @@ -2,10 +2,15 @@ //tambahkan dengan pangkat 2 (n**2) //source: https://www.codewars.com/kata/515e271a311df0350d00000f -function squareSum(numbers){ +function squareSum(numbers) { //code here + let sum = 0; + for (let i = 0; i < numbers.length; i++) { + sum += numbers[i] ** 2; + } + return sum; } -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..06a286c 100644 --- a/Loop-Number/centuryYear.js +++ b/Loop-Number/centuryYear.js @@ -5,12 +5,18 @@ //source: https://www.codewars.com/kata/5a3fe3dde1ce0e8ed6000097 function century(year) { - // Finish this :) - return; + // const str = String(parseInt(year / 100)); + + let result = 0; + while (year >= 1) { + result += 1; + year -= 100; + } + return result; } 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..b7b2167 100644 --- a/Loop-Number/summation.js +++ b/Loop-Number/summation.js @@ -3,10 +3,15 @@ //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 sum = 0; + for (let i = 1; i <= num; i++) { + sum += i; + } + return sum; } 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..cdffee8 100644 --- a/Loop-String/abbrevName.js +++ b/Loop-String/abbrevName.js @@ -3,12 +3,17 @@ // patrick feeney => P.F //source: https://www.codewars.com/kata/57eadb7ecd143f4c9c0000a3 -function abbrevName(name){ - //code here +function abbrevName(name) { + let result = ""; + for (let i = 0; i < name.length; i++) { + if (/[A-Z]/.test(name[i])) result += name[i]; + if (name[i] === " ") result += `.`; + } + return result; } 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..b3f88f3 100644 --- a/Loop-String/noSpace.js +++ b/Loop-String/noSpace.js @@ -2,10 +2,14 @@ //hilangkan spasinya saja //source: https://www.codewars.com/kata/57eae20f5500ad98e50002c5 -function noSpace(x){ +function noSpace(x) { //code here - + let result = ""; + for (let i = 0; i < x.length; i++) { + if (x[i] !== " ") result += x[i]; + } + return result; } -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..255979e 100644 --- a/Loop-String/removeChar.js +++ b/Loop-String/removeChar.js @@ -2,13 +2,17 @@ //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) result += str[i]; + } + 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 \ No newline at end of file +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..bafd163 100644 --- a/Loop-String/stringRepeat.js +++ b/Loop-String/stringRepeat.js @@ -2,15 +2,20 @@ //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 = n > 0 ? s : ""; + for (let 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..9a19f13 100644 --- a/Pattern-Printing/diamond.js +++ b/Pattern-Printing/diamond.js @@ -1,7 +1,16 @@ function berlian(num) { - let pattern = ''; // code here - return pattern; + for (let i = 1; i <= num; i++) { + let pattern = ""; + + for (let j = 1; j <= num - i; j++) { + pattern += " "; + } + for (let k = 1; k <= 2 * i - 1; k++) { + pattern += "*"; + } + console.log(pattern); + } } // use case @@ -17,4 +26,4 @@ expected result ***** *** * -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/papanCatur.js b/Pattern-Printing/papanCatur.js index c90644e..8b6895b 100644 --- a/Pattern-Printing/papanCatur.js +++ b/Pattern-Printing/papanCatur.js @@ -2,9 +2,14 @@ // Petunjuk: Gunakan logika 'ganjil-genap' dari // penjumlahan indeks baris (i) dan kolom (j). function papanCatur(num) { - let pattern = ''; - // code here - return pattern; + let pattern = ""; + for (let i = 0; i <= num; i++) { + for (let i = 0; i < num; i++) { + if (i % 2) pattern += " "; + else pattern += "#"; + } + console.log(pattern); + } } // use case @@ -16,4 +21,4 @@ expected result # # # # # # # # -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/tempCodeRunnerFile.js b/Pattern-Printing/tempCodeRunnerFile.js new file mode 100644 index 0000000..2d070bf --- /dev/null +++ b/Pattern-Printing/tempCodeRunnerFile.js @@ -0,0 +1,15 @@ +for (let i = num - 1; i >= 1; i--) { + // let row = ""; + + // // 1. Print Spaces + // for (let j = 1; j <= num - i; j++) { + // row += " "; + // } + + // // 2. Print Stars + // for (let k = 1; k <= 2 * i - 1; k++) { + // row += "*"; + // } + + // console.log(row); + // } \ No newline at end of file From 06c3f92f97c858c20afd442dda12e70a4327e81e Mon Sep 17 00:00:00 2001 From: zxcvbnm0091 Date: Sat, 3 Jan 2026 15:03:04 +0700 Subject: [PATCH 2/3] done --- Pattern-Printing/diamond.js | 10 ++++++++++ Pattern-Printing/papanCatur.js | 13 ++++++++----- Pattern-Printing/patternX.js | 18 +++++++++++++----- Pattern-Printing/persegi.js | 12 ++++++++---- Pattern-Printing/persegiBolong.js | 18 ++++++++++++++---- Pattern-Printing/piramid.js | 14 ++++++++++++-- Pattern-Printing/piramid2.js | 14 ++++++++++++-- Pattern-Printing/piramidTerbalik.js | 15 ++++++++++++--- Pattern-Printing/segitigaSiku.js | 12 ++++++++++-- Pattern-Printing/segitigaSikuTerbalik.js | 12 ++++++++++-- Pattern-Printing/tempCodeRunnerFile.js | 15 --------------- 11 files changed, 109 insertions(+), 44 deletions(-) delete mode 100644 Pattern-Printing/tempCodeRunnerFile.js diff --git a/Pattern-Printing/diamond.js b/Pattern-Printing/diamond.js index 9a19f13..0d76eb6 100644 --- a/Pattern-Printing/diamond.js +++ b/Pattern-Printing/diamond.js @@ -11,6 +11,16 @@ function berlian(num) { } console.log(pattern); } + for (let i = num; i > 1; i--) { + let pattern = ""; + for (let j = 1; j <= num - (i - 1); j++) { + pattern += " "; + } + for (let k = 1; k <= 2 * (i - 1) - 1; k++) { + pattern += "*"; + } + console.log(pattern); + } } // use case diff --git a/Pattern-Printing/papanCatur.js b/Pattern-Printing/papanCatur.js index 8b6895b..bc1a2b3 100644 --- a/Pattern-Printing/papanCatur.js +++ b/Pattern-Printing/papanCatur.js @@ -2,11 +2,14 @@ // Petunjuk: Gunakan logika 'ganjil-genap' dari // penjumlahan indeks baris (i) dan kolom (j). function papanCatur(num) { - let pattern = ""; - for (let i = 0; i <= num; i++) { - for (let i = 0; i < num; i++) { - if (i % 2) pattern += " "; - else pattern += "#"; + for (let i = 1; i <= num; i++) { + let pattern = ""; + for (let j = 1; j <= num; j++) { + if ((i % 2 && j % 2) || (i % 2 === 0 && j % 2 === 0)) { + pattern += "#"; + } else { + pattern += " "; + } } console.log(pattern); } diff --git a/Pattern-Printing/patternX.js b/Pattern-Printing/patternX.js index b191574..8fcb33f 100644 --- a/Pattern-Printing/patternX.js +++ b/Pattern-Printing/patternX.js @@ -3,13 +3,21 @@ // berada di diagonal utama (i === j) ATAU // diagonal terbalik (i + j === num - 1). function polaX(num) { - let pattern = ''; - // code here - return pattern; + for (let i = 1; i <= num; i++) { + let pattern = ""; + for (let j = 1; j <= num; j++) { + if (i === j || i + j === num + 1) { + pattern += "*"; + } else { + pattern += " "; + } + } + console.log(pattern); + } } // use case -console.log('\n--- Soal 11: Pola X ---'); +console.log("\n--- Soal 11: Pola X ---"); console.log(polaX(5)); /* expected result @@ -18,4 +26,4 @@ expected result * * * * * -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/persegi.js b/Pattern-Printing/persegi.js index 1c28f6d..f1e2e14 100644 --- a/Pattern-Printing/persegi.js +++ b/Pattern-Printing/persegi.js @@ -1,8 +1,12 @@ // Wajib menggunakan nested loop function persegi(num) { - let pattern = ''; - // code here - return pattern; + for (let i = 1; i <= num; i++) { + let pattern = ""; + for (let j = 1; j <= num; j++) { + pattern += "*"; + } + console.log(pattern); + } } // use case @@ -13,4 +17,4 @@ expected result **** **** **** -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/persegiBolong.js b/Pattern-Printing/persegiBolong.js index 398fc02..7c891f7 100644 --- a/Pattern-Printing/persegiBolong.js +++ b/Pattern-Printing/persegiBolong.js @@ -1,8 +1,18 @@ // Wajib menggunakan nested loop dan conditional (if/else) di dalamnya. function persegiBolong(num) { - let pattern = ''; - // code here - return pattern; + let result = ""; + for (let i = 1; i <= num; i++) { + let pattern = ""; + for (let j = 1; j <= num; j++) { + if (i === 1 || i == num || j === 1 || j === num) { + pattern += "*"; + } else { + pattern += " "; + } + } + result += pattern + "\n"; + } + return result; } // use case @@ -14,4 +24,4 @@ expected result * * * * ***** -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/piramid.js b/Pattern-Printing/piramid.js index afa2b5d..4d698ee 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++) { + let row = ""; + for (let j = 1; j <= num - i; j++) { + row += " "; + } + for (let k = 1; k <= 2 * i - 1; k++) { + row += "*"; + } + pattern += row + "\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..18f171c 100644 --- a/Pattern-Printing/piramid2.js +++ b/Pattern-Printing/piramid2.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 piramida2(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = 1; i <= num; i++) { + let row = ""; + for (let j = 1; j <= num - i; j++) { + row += " "; + } + for (let k = 1; k <= i; k++) { + row += "* "; + } + pattern += row + "\n"; + } return pattern; } @@ -15,4 +25,4 @@ expected result * * * * * * * * * * * * -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/piramidTerbalik.js b/Pattern-Printing/piramidTerbalik.js index 8c5677e..61f5a03 100644 --- a/Pattern-Printing/piramidTerbalik.js +++ b/Pattern-Printing/piramidTerbalik.js @@ -1,6 +1,15 @@ function piramidaTerbalik(num) { - let pattern = ''; - // code here + let pattern = ""; + for (let i = num; i >= 1; i--) { + let row = ""; + for (let j = 1; j <= num - i; j++) { + row += " "; + } + for (let k = 1; k <= 2 * i - 1; k++) { + row += "*"; + } + pattern += row + "\n"; + } return pattern; } @@ -13,4 +22,4 @@ expected result ***** *** * -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/segitigaSiku.js b/Pattern-Printing/segitigaSiku.js index c5cce1e..d5b0865 100644 --- a/Pattern-Printing/segitigaSiku.js +++ b/Pattern-Printing/segitigaSiku.js @@ -1,7 +1,15 @@ // Pada quiz ini wajib menggunakan nested loop function segitigaSiku(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = 1; i <= num; i++) { + let row = ""; + for (let j = 1; j <= i; j++) { + row += "*"; + } + + pattern += row + "\n"; + } return pattern; } @@ -14,4 +22,4 @@ expected result *** **** ***** -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/segitigaSikuTerbalik.js b/Pattern-Printing/segitigaSikuTerbalik.js index 287c2dd..1872280 100644 --- a/Pattern-Printing/segitigaSikuTerbalik.js +++ b/Pattern-Printing/segitigaSikuTerbalik.js @@ -1,7 +1,15 @@ // Wajib menggunakan nested loop function segitigaTerbalik(num) { - let pattern = ''; + let pattern = ""; // code here + for (let i = num; i >= 1; i--) { + let row = ""; + for (let j = 1; j <= i; j++) { + row += "*"; + } + + pattern += row + "\n"; + } return pattern; } @@ -14,4 +22,4 @@ expected result *** ** * -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/tempCodeRunnerFile.js b/Pattern-Printing/tempCodeRunnerFile.js deleted file mode 100644 index 2d070bf..0000000 --- a/Pattern-Printing/tempCodeRunnerFile.js +++ /dev/null @@ -1,15 +0,0 @@ -for (let i = num - 1; i >= 1; i--) { - // let row = ""; - - // // 1. Print Spaces - // for (let j = 1; j <= num - i; j++) { - // row += " "; - // } - - // // 2. Print Stars - // for (let k = 1; k <= 2 * i - 1; k++) { - // row += "*"; - // } - - // console.log(row); - // } \ No newline at end of file From da7e6fb6545b0dec029ecb6bd54b0a2b74fa46aa Mon Sep 17 00:00:00 2001 From: zxcvbnm0091 Date: Sat, 3 Jan 2026 15:25:42 +0700 Subject: [PATCH 3/3] done --- Loop-Array/findHighest.js | 10 +++++----- Loop-String/abbrevName.js | 6 ++++-- Pattern-Printing/diamond.js | 18 ++++++++++-------- Pattern-Printing/papanCatur.js | 10 ++++++---- Pattern-Printing/patternX.js | 10 ++++++---- Pattern-Printing/persegi.js | 8 +++++--- Pattern-Printing/persegiBolong.js | 12 ++++++------ 7 files changed, 42 insertions(+), 32 deletions(-) diff --git a/Loop-Array/findHighest.js b/Loop-Array/findHighest.js index c55da40..136608a 100644 --- a/Loop-Array/findHighest.js +++ b/Loop-Array/findHighest.js @@ -12,8 +12,8 @@ function findHighestInt(arr) { 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 +console.log(findHighestInt([78, 56, 232, 12, 8])); //232 +console.log(findHighestInt([78, 56, 232, 12, 18])); //232 +console.log(findHighestInt([78, 56, 232, 412, 228])); //412 +console.log(findHighestInt([78, 56, 232, 12, 0])); //232 +console.log(findHighestInt([1, 56, 232, 12, 8])); //232 diff --git a/Loop-String/abbrevName.js b/Loop-String/abbrevName.js index cdffee8..2258123 100644 --- a/Loop-String/abbrevName.js +++ b/Loop-String/abbrevName.js @@ -6,8 +6,10 @@ function abbrevName(name) { let result = ""; for (let i = 0; i < name.length; i++) { - if (/[A-Z]/.test(name[i])) result += name[i]; - if (name[i] === " ") result += `.`; + if (name[i] === " ") result += "."; + if (i === 0 || name[i - 1] === " ") { + result += name[i]; + } } return result; } diff --git a/Pattern-Printing/diamond.js b/Pattern-Printing/diamond.js index 0d76eb6..5ee6082 100644 --- a/Pattern-Printing/diamond.js +++ b/Pattern-Printing/diamond.js @@ -1,26 +1,28 @@ function berlian(num) { // code here + let pattern = ""; for (let i = 1; i <= num; i++) { - let pattern = ""; + let row = ""; for (let j = 1; j <= num - i; j++) { - pattern += " "; + row += " "; } for (let k = 1; k <= 2 * i - 1; k++) { - pattern += "*"; + row += "*"; } - console.log(pattern); + pattern += row + "\n"; } for (let i = num; i > 1; i--) { - let pattern = ""; + let row = ""; for (let j = 1; j <= num - (i - 1); j++) { - pattern += " "; + row += " "; } for (let k = 1; k <= 2 * (i - 1) - 1; k++) { - pattern += "*"; + row += "*"; } - console.log(pattern); + pattern += row + "\n"; } + return pattern; } // use case diff --git a/Pattern-Printing/papanCatur.js b/Pattern-Printing/papanCatur.js index bc1a2b3..b05372e 100644 --- a/Pattern-Printing/papanCatur.js +++ b/Pattern-Printing/papanCatur.js @@ -2,17 +2,19 @@ // Petunjuk: Gunakan logika 'ganjil-genap' dari // penjumlahan indeks baris (i) dan kolom (j). function papanCatur(num) { + let pattern = ""; for (let i = 1; i <= num; i++) { - let pattern = ""; + let row = ""; for (let j = 1; j <= num; j++) { if ((i % 2 && j % 2) || (i % 2 === 0 && j % 2 === 0)) { - pattern += "#"; + row += "#"; } else { - pattern += " "; + row += " "; } } - console.log(pattern); + pattern += row + "\n"; } + return pattern; } // use case diff --git a/Pattern-Printing/patternX.js b/Pattern-Printing/patternX.js index 8fcb33f..0663693 100644 --- a/Pattern-Printing/patternX.js +++ b/Pattern-Printing/patternX.js @@ -3,17 +3,19 @@ // berada di diagonal utama (i === j) ATAU // diagonal terbalik (i + j === num - 1). function polaX(num) { + let pattern = ""; for (let i = 1; i <= num; i++) { - let pattern = ""; + let row = ""; for (let j = 1; j <= num; j++) { if (i === j || i + j === num + 1) { - pattern += "*"; + row += "*"; } else { - pattern += " "; + row += " "; } } - console.log(pattern); + pattern += row + "\n"; } + return pattern; } // use case diff --git a/Pattern-Printing/persegi.js b/Pattern-Printing/persegi.js index f1e2e14..75aebb7 100644 --- a/Pattern-Printing/persegi.js +++ b/Pattern-Printing/persegi.js @@ -1,12 +1,14 @@ // Wajib menggunakan nested loop function persegi(num) { + let pattern = ""; for (let i = 1; i <= num; i++) { - let pattern = ""; + let row = ""; for (let j = 1; j <= num; j++) { - pattern += "*"; + row += "*"; } - console.log(pattern); + pattern += row + "\n"; } + return pattern; } // use case diff --git a/Pattern-Printing/persegiBolong.js b/Pattern-Printing/persegiBolong.js index 7c891f7..79a70fa 100644 --- a/Pattern-Printing/persegiBolong.js +++ b/Pattern-Printing/persegiBolong.js @@ -1,18 +1,18 @@ // Wajib menggunakan nested loop dan conditional (if/else) di dalamnya. function persegiBolong(num) { - let result = ""; + let pattern = ""; for (let i = 1; i <= num; i++) { - let pattern = ""; + let row = ""; for (let j = 1; j <= num; j++) { if (i === 1 || i == num || j === 1 || j === num) { - pattern += "*"; + row += "*"; } else { - pattern += " "; + row += " "; } } - result += pattern + "\n"; + pattern += row + "\n"; } - return result; + return pattern; } // use case