diff --git a/Loop-Array/countingSheep.js b/Loop-Array/countingSheep.js index e0ee29a..9ce52b7 100644 --- a/Loop-Array/countingSheep.js +++ b/Loop-Array/countingSheep.js @@ -4,8 +4,19 @@ function countSheeps(sheep) { // TODO + let benar = 0; + + //looping + for(let i=0; i < sheep.length; i++ ){ + if(sheep[i] == true){ + benar++; + } + } + return benar; } + + console.log(countSheeps([])) //0 console.log(countSheeps([undefined])) //0 console.log(countSheeps([null])) //0 @@ -17,5 +28,4 @@ console.log(countSheeps( true,false,true,false, true,false,false,true, true,true,true,true, - false,false,true,true -])) //17 \ No newline at end of file + false,false,true,true])) //17 \ No newline at end of file diff --git a/Loop-Array/findHighest.js b/Loop-Array/findHighest.js index ffb59a5..73b68a8 100644 --- a/Loop-Array/findHighest.js +++ b/Loop-Array/findHighest.js @@ -4,6 +4,15 @@ function findHighestInt(arr) { //your code here + let high = 0; + + //Loop + for(let i = 0; i <= arr.length; i++){ + if(arr[i] > high){ + high = arr[i]; + } + } + return high; } diff --git a/Loop-Array/findNeedle.js b/Loop-Array/findNeedle.js index d4c9660..7d5b6f5 100644 --- a/Loop-Array/findNeedle.js +++ b/Loop-Array/findNeedle.js @@ -4,6 +4,12 @@ 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}` + } + } + } let haystack_1 = ['3', '123124234', undefined, 'needle', 'world', 'hay', 2, '3', true, false]; diff --git a/Loop-Array/findSmallest.js b/Loop-Array/findSmallest.js index ad42028..e58733c 100644 --- a/Loop-Array/findSmallest.js +++ b/Loop-Array/findSmallest.js @@ -4,7 +4,15 @@ function findSmallestInt(arr) { //your code here - + let kecil = 500; + + // Loop + for(let i = 0; i <= arr.length; i++){ + if(arr[i] < kecil){ + kecil = arr[i]; + } + } + return kecil; } console.log(findSmallestInt([78,56,232,12,8])); //8 diff --git a/Loop-Array/positifSum.js b/Loop-Array/positifSum.js index 6483e00..3ea11d2 100644 --- a/Loop-Array/positifSum.js +++ b/Loop-Array/positifSum.js @@ -4,6 +4,15 @@ function positiveSum(arr) { //code here + let cum = 0; + + //Looping + for(let i = 0; i <= arr.length; i++){ + if(arr[i] > 0){ + cum = cum + arr[i]; + } + } + return cum; } console.log(positiveSum([1,2,3,4,5])) //15 diff --git a/Loop-Array/squareSum.js b/Loop-Array/squareSum.js index 9d07259..c750fa1 100644 --- a/Loop-Array/squareSum.js +++ b/Loop-Array/squareSum.js @@ -4,6 +4,12 @@ function squareSum(numbers){ //code here + let hasil = 0; + for(let i = 0; i < numbers.length; i++){ + let angka = numbers[i] **2; + hasil = hasil + angka; + } + return hasil; } console.log(squareSum([1,2])); //5 diff --git a/Loop-Number/centuryYear.js b/Loop-Number/centuryYear.js index 296af71..8e16615 100644 --- a/Loop-Number/centuryYear.js +++ b/Loop-Number/centuryYear.js @@ -6,7 +6,12 @@ function century(year) { // Finish this :) - return; + let abad = 1; + while (year > 100){ + year = year - 100; + abad = abad + 1; + } + return abad; } console.log(century(1705)); //18 diff --git a/Loop-Number/summation.js b/Loop-Number/summation.js index 327ab74..d33d200 100644 --- a/Loop-Number/summation.js +++ b/Loop-Number/summation.js @@ -5,6 +5,11 @@ function summation (num) { // Code here + let angka = 0; + for(let i = 1; i <= num; i++){ + angka = angka + i; + } + return angka; } console.log(summation(1)); //1 diff --git a/Loop-String/abbrevName.js b/Loop-String/abbrevName.js index 48124c2..3627d7a 100644 --- a/Loop-String/abbrevName.js +++ b/Loop-String/abbrevName.js @@ -5,6 +5,12 @@ function abbrevName(name){ //code here + let hasil = ""; + for(let i = 0; i <= name.length; i++){ + if(name[i] >= 'A' && name[i] <= 'Z') hasil += name[i]; + if(name[i] === " ") hasil += "."; + } + return hasil; } console.log(abbrevName("Sam Harris")); //S.H diff --git a/Loop-String/noSpace.js b/Loop-String/noSpace.js index e62ba23..6cc38bf 100644 --- a/Loop-String/noSpace.js +++ b/Loop-String/noSpace.js @@ -4,6 +4,12 @@ function noSpace(x){ //code here + let hasil = ""; + + for(let i = 0; i <= x.length; i++){ + if(x[i] !==" ") hasil += x[i]; + } + return hasil } diff --git a/Loop-String/removeChar.js b/Loop-String/removeChar.js index 2c09bed..a86a016 100644 --- a/Loop-String/removeChar.js +++ b/Loop-String/removeChar.js @@ -3,8 +3,14 @@ //source: https://www.codewars.com/kata/56bc28ad5bdaeb48760009b0 function removeChar(str){ - -}; + let hasil = ""; + for(let i = 0; i <= str.length; i++){ + if(i > 0 && i < str.length-1){ + hasil += str[i]; + } + } + return hasil; +} console.log(removeChar('eloquent')); //loquen console.log(removeChar('country')); //ountr diff --git a/Loop-String/stringRepeat.js b/Loop-String/stringRepeat.js index ca7175f..5b502db 100644 --- a/Loop-String/stringRepeat.js +++ b/Loop-String/stringRepeat.js @@ -4,6 +4,11 @@ function repeatStr (n, s) { //code here + let hasil = ""; + for(let i = 0; i < n; i++){ + hasil = hasil + n; + } + return hasil; } console.log(repeatStr(3, "*")); //*** diff --git a/Pattern-Printing/diamond.js b/Pattern-Printing/diamond.js index a813fb7..e63f80d 100644 --- a/Pattern-Printing/diamond.js +++ b/Pattern-Printing/diamond.js @@ -1,6 +1,36 @@ function berlian(num) { - let pattern = ''; + // code here + let pattern = ""; + + //loop segitiga atas + for(let i = 0; i < num; i++){ + //Loop Spasi + for(let j = 0; j < num - i - 1; j++){ + pattern += " "; + } + // Loop Bintang + for(let k = 0; k < 2 * i + 1; k++){ + pattern += "*"; + } + pattern += `\n`; + } + //Loop Bawah + for (let i = num - 2; i >= 0; i--) { + + // spasi + for (let j = 0; j < num - i - 1; j++) { + pattern += ' '; + } + + // bintang + for (let k = 0; k < 2 * i + 1; k++) { + pattern += '*'; + } + + + pattern += '\n'; + } return pattern; } diff --git a/Pattern-Printing/papanCatur.js b/Pattern-Printing/papanCatur.js index c90644e..eaa81a8 100644 --- a/Pattern-Printing/papanCatur.js +++ b/Pattern-Printing/papanCatur.js @@ -4,6 +4,16 @@ function papanCatur(num) { let pattern = ''; // code here + for(let i = 0; i < num; i++){ + for(let j = 0; j < num; j++){ + if((i+j) % 2 == 0){ + pattern += `#`; + }else{ + pattern += ` `; + } + } + pattern += '\n'; + } return pattern; } diff --git a/Pattern-Printing/patternX.js b/Pattern-Printing/patternX.js index b191574..6f8e0bb 100644 --- a/Pattern-Printing/patternX.js +++ b/Pattern-Printing/patternX.js @@ -5,6 +5,16 @@ function polaX(num) { 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; } diff --git a/Pattern-Printing/persegi.js b/Pattern-Printing/persegi.js index 1c28f6d..728ffd8 100644 --- a/Pattern-Printing/persegi.js +++ b/Pattern-Printing/persegi.js @@ -2,6 +2,12 @@ function persegi(num) { let pattern = ''; // code here + for(let i = 0 ; i < num; i++){ + for(let j = 0; j < num; j++){ + pattern += '*'; + } + pattern += '\n'; + } return pattern; } diff --git a/Pattern-Printing/persegiBolong.js b/Pattern-Printing/persegiBolong.js index 398fc02..fe3a9c4 100644 --- a/Pattern-Printing/persegiBolong.js +++ b/Pattern-Printing/persegiBolong.js @@ -2,6 +2,16 @@ function persegiBolong(num) { let pattern = ''; // code here + for(let i = 0; i < num; i++){ + for(let j = 0; j < num; j++){ + if(i == 0 || i == num -1 || j == 0 || j == num-1){ + pattern += '*'; + }else{ + pattern += ' '; + } + } + pattern += '\n'; + } return pattern; } diff --git a/Pattern-Printing/piramid.js b/Pattern-Printing/piramid.js index afa2b5d..33ef347 100644 --- a/Pattern-Printing/piramid.js +++ b/Pattern-Printing/piramid.js @@ -3,9 +3,20 @@ function piramida(num) { let pattern = ''; // code here + for(let i = 0; i < num ; i++){ + for(let j = 0; j < num - i - 1; j++){ + pattern += ' '; + } + for (let k = 0; k < 2 * i + 1; k++) { + pattern += '*'; + } + + pattern += '\n'; + } return pattern; } + // use case console.log(piramida(5)); /* diff --git a/Pattern-Printing/piramid2.js b/Pattern-Printing/piramid2.js index 14e316d..2ab2fe5 100644 --- a/Pattern-Printing/piramid2.js +++ b/Pattern-Printing/piramid2.js @@ -3,6 +3,16 @@ function piramida2(num) { let pattern = ''; // code here + for(let i = 0; i < num ; i++){ + for(let j = 0; j < num - i - 1; j++){ + pattern += ' '; + } + for (let k = 0; k < i ; k++) { + pattern += '* '; + } + pattern += '*'; + pattern += '\n'; + } return pattern; } diff --git a/Pattern-Printing/piramidTerbalik.js b/Pattern-Printing/piramidTerbalik.js index 8c5677e..ca1a9f8 100644 --- a/Pattern-Printing/piramidTerbalik.js +++ b/Pattern-Printing/piramidTerbalik.js @@ -1,6 +1,18 @@ function piramidaTerbalik(num) { let pattern = ''; // code here + for(let i = 0; i < num ; i++){ + + for(let j = 0; j < i; j++){ + pattern += ' '; + } + + for (let k = 0; k < 2 * (num - i) - 1; k++) { + pattern += '*'; + } + + pattern += '\n'; + } return pattern; } @@ -9,7 +21,7 @@ console.log(piramidaTerbalik(5)); /* expected result ********* - ******* + ******* ***** *** * diff --git a/Pattern-Printing/segitigaSiku.js b/Pattern-Printing/segitigaSiku.js index c5cce1e..4ffc505 100644 --- a/Pattern-Printing/segitigaSiku.js +++ b/Pattern-Printing/segitigaSiku.js @@ -2,6 +2,12 @@ function segitigaSiku(num) { let pattern = ''; // code here + for(let i = 0; i <= num; i++){ + for(let j = 0; j < i; j++){ + pattern += '*'; + } + pattern += '\n'; + } return pattern; } diff --git a/Pattern-Printing/segitigaSikuTerbalik.js b/Pattern-Printing/segitigaSikuTerbalik.js index 287c2dd..f4005e6 100644 --- a/Pattern-Printing/segitigaSikuTerbalik.js +++ b/Pattern-Printing/segitigaSikuTerbalik.js @@ -2,6 +2,13 @@ function segitigaTerbalik(num) { let pattern = ''; // code here + for(let i = 0; i < num ; i++){ + for (let k = 0; k < num - i ; k++) { + pattern += '*'; + } + + pattern += '\n'; + } return pattern; }