Skip to content
14 changes: 12 additions & 2 deletions Loop-Array/countingSheep.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,5 +28,4 @@ console.log(countSheeps(
true,false,true,false,
true,false,false,true,
true,true,true,true,
false,false,true,true
])) //17
false,false,true,true])) //17
9 changes: 9 additions & 0 deletions Loop-Array/findHighest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

Expand Down
6 changes: 6 additions & 0 deletions Loop-Array/findNeedle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
10 changes: 9 additions & 1 deletion Loop-Array/findSmallest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions Loop-Array/positifSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Loop-Array/squareSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion Loop-Number/centuryYear.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Loop-Number/summation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Loop-String/abbrevName.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Loop-String/noSpace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

Expand Down
10 changes: 8 additions & 2 deletions Loop-String/removeChar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Loop-String/stringRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, "*")); //***
Expand Down
32 changes: 31 additions & 1 deletion Pattern-Printing/diamond.js
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions Pattern-Printing/papanCatur.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions Pattern-Printing/patternX.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions Pattern-Printing/persegi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions Pattern-Printing/persegiBolong.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
11 changes: 11 additions & 0 deletions Pattern-Printing/piramid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
/*
Expand Down
10 changes: 10 additions & 0 deletions Pattern-Printing/piramid2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 13 additions & 1 deletion Pattern-Printing/piramidTerbalik.js
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -9,7 +21,7 @@ console.log(piramidaTerbalik(5));
/*
expected result
*********
*******
*******
*****
***
*
Expand Down
6 changes: 6 additions & 0 deletions Pattern-Printing/segitigaSiku.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions Pattern-Printing/segitigaSikuTerbalik.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down