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..136608a 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])); //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-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..2258123 100644 --- a/Loop-String/abbrevName.js +++ b/Loop-String/abbrevName.js @@ -3,12 +3,19 @@ // 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 (name[i] === " ") result += "."; + if (i === 0 || name[i - 1] === " ") { + result += name[i]; + } + } + 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..5ee6082 100644 --- a/Pattern-Printing/diamond.js +++ b/Pattern-Printing/diamond.js @@ -1,6 +1,27 @@ function berlian(num) { - let pattern = ''; // code here + let pattern = ""; + 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"; + } + for (let i = num; i > 1; i--) { + let row = ""; + for (let j = 1; j <= num - (i - 1); j++) { + row += " "; + } + for (let k = 1; k <= 2 * (i - 1) - 1; k++) { + row += "*"; + } + pattern += row + "\n"; + } return pattern; } @@ -17,4 +38,4 @@ expected result ***** *** * -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/papanCatur.js b/Pattern-Printing/papanCatur.js index c90644e..b05372e 100644 --- a/Pattern-Printing/papanCatur.js +++ b/Pattern-Printing/papanCatur.js @@ -2,8 +2,18 @@ // Petunjuk: Gunakan logika 'ganjil-genap' dari // penjumlahan indeks baris (i) dan kolom (j). function papanCatur(num) { - let pattern = ''; - // code here + let pattern = ""; + for (let i = 1; i <= num; i++) { + let row = ""; + for (let j = 1; j <= num; j++) { + if ((i % 2 && j % 2) || (i % 2 === 0 && j % 2 === 0)) { + row += "#"; + } else { + row += " "; + } + } + pattern += row + "\n"; + } return pattern; } @@ -16,4 +26,4 @@ expected result # # # # # # # # -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/patternX.js b/Pattern-Printing/patternX.js index b191574..0663693 100644 --- a/Pattern-Printing/patternX.js +++ b/Pattern-Printing/patternX.js @@ -3,13 +3,23 @@ // berada di diagonal utama (i === j) ATAU // diagonal terbalik (i + j === num - 1). function polaX(num) { - let pattern = ''; - // code here + let pattern = ""; + for (let i = 1; i <= num; i++) { + let row = ""; + for (let j = 1; j <= num; j++) { + if (i === j || i + j === num + 1) { + row += "*"; + } else { + row += " "; + } + } + pattern += row + "\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 @@ -18,4 +28,4 @@ expected result * * * * * -*/ \ No newline at end of file +*/ diff --git a/Pattern-Printing/persegi.js b/Pattern-Printing/persegi.js index 1c28f6d..75aebb7 100644 --- a/Pattern-Printing/persegi.js +++ b/Pattern-Printing/persegi.js @@ -1,7 +1,13 @@ // Wajib menggunakan nested loop function persegi(num) { - let pattern = ''; - // code here + let pattern = ""; + for (let i = 1; i <= num; i++) { + let row = ""; + for (let j = 1; j <= num; j++) { + row += "*"; + } + pattern += row + "\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..79a70fa 100644 --- a/Pattern-Printing/persegiBolong.js +++ b/Pattern-Printing/persegiBolong.js @@ -1,7 +1,17 @@ // Wajib menggunakan nested loop dan conditional (if/else) di dalamnya. function persegiBolong(num) { - let pattern = ''; - // code here + let pattern = ""; + for (let i = 1; i <= num; i++) { + let row = ""; + for (let j = 1; j <= num; j++) { + if (i === 1 || i == num || j === 1 || j === num) { + row += "*"; + } else { + row += " "; + } + } + pattern += row + "\n"; + } return pattern; } @@ -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 +*/