Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Loop-Array/countingSheep.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

function countSheeps(sheep) {
// TODO
let jumlah =0;
for (let i = 0; i < sheep.length;i++) {
if (sheep[i] === true) {
jumlah++
}
}
return jumlah;
}

console.log(countSheeps([])) //0
Expand Down
9 changes: 8 additions & 1 deletion Loop-Array/findHighest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

function findHighestInt(arr) {
//your code here

for ( let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - 1; j++) {
if (arr[j] < arr[j+1]) {
[arr[j], arr[j+1]] = [arr[j+1], arr[j]];
}
}
}
return arr;
}

console.log(findHighestInt([78,56,232,12,8])); //8
Expand Down
8 changes: 8 additions & 0 deletions Loop-Array/findNeedle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

function findNeedle(haystack) {
// your code here
let pocici = 0;
for (let i = 0; i < haystack.length;i++) {
if (haystack[i] === 'needle') {
pocici+=i;
break;
}
}
return `found the needle at position ${pocici}`;
}

let haystack_1 = ['3', '123124234', undefined, 'needle', 'world', 'hay', 2, '3', true, false];
Expand Down
8 changes: 8 additions & 0 deletions Loop-Array/findSmallest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

function findSmallestInt(arr) {
//your code here
for (let pariabel1 = 0; pariabel1 < arr.length; pariabel1++) { //sorry bang gabut hehehe :)
for (let pariabel2 = 0; pariabel2 < arr.length -1; pariabel2++) {
if (arr[pariabel2] > arr[pariabel2 + 1]) {
[arr[pariabel2], arr[pariabel2 + 1]] = [arr[pariabel2 + 1], arr[pariabel2]];
}
}
}
return arr;

}

Expand Down
8 changes: 8 additions & 0 deletions Loop-Array/positifSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

function positiveSum(arr) {
//code here
hacil_akir = 0;
for (let pariabel = 0; pariabel < arr.length; pariabel++) {
// gabut guys :)
if (arr[pariabel] > 0) {
hacil_akir+=arr[pariabel];
}
}
return hacil_akir;
}

console.log(positiveSum([1,2,3,4,5])) //15
Expand Down
7 changes: 7 additions & 0 deletions Loop-Array/squareSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

function squareSum(numbers){
//code here
let hasil = 0;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] != 0) {
hasil = hasil + numbers[i] ** 2;
}
}
return hasil;
}

console.log(squareSum([1,2])); //5
Expand Down
6 changes: 5 additions & 1 deletion Loop-Number/centuryYear.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

function century(year) {
// Finish this :)
return;
abad = 1;
for (let i = 1; i < year/100; i++) {
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 hasil = 0;
for (let i = 0; i < num; i++) {
hasil += num - i;
}
return hasil;
}

console.log(summation(1)); //1
Expand Down
9 changes: 9 additions & 0 deletions Loop-String/abbrevName.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@

function abbrevName(name){
//code here
output = "";
for (let i=0; i < name.length; i++){
if (name[i] == name[0]) {
output+=name[0];
} else if (name[i] === " "){
output+="."+name[i+1];
}
}
return output;
}

console.log(abbrevName("Sam Harris")); //S.H
Expand Down
10 changes: 9 additions & 1 deletion Loop-String/noSpace.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

function noSpace(x){
//code here

hasil = "";
for (let i = 0; i < x.length; i++) {
if (x[i] == " ") {
continue;
} else {
hasil+=x[i];
}
}
return hasil;
}

console.log(noSpace("8 j 8 mBliB8g imjB8B8 jl B")) //8j8mBliB8gimjB8B8jlB
Expand Down
14 changes: 12 additions & 2 deletions Loop-String/removeChar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
//source: https://www.codewars.com/kata/56bc28ad5bdaeb48760009b0

function removeChar(str){

};
let hasil = "";
for (let i = 0; i < str.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sebenernya start i = 1 dan i < str.length - 1 itu juga solusi yg lebih clean dan tanpa perlu if else

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tapi good job, ini juga termasuk benar

if (i == 0) {
continue;
} else if (i == str.length - 1) {
continue;
} else {
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 = 1; i <= n; i++ ) {
hasil = hasil + s;
}
return hasil;
}

console.log(repeatStr(3, "*")); //***
Expand Down
20 changes: 20 additions & 0 deletions Pattern-Printing/diamond.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
function berlian(num) {
let pattern = '';
// code here
for (let i = num; i > 0 ;i--) {
for (let q = 1; q <= num * 2 - i ; q++) {
if (q < i){
pattern+=" ";
} else {
pattern+="*";
}
}
pattern+="\n";
}
for (let j = 1; j <= num; j++) {
for (let r = 0; r < num * 2 - j - 1;r++) {
if (r < j ) {
pattern+=" ";
} else {
pattern+="*";
}
}
pattern+="\n";
}
return pattern;
}

Expand Down
22 changes: 22 additions & 0 deletions Pattern-Printing/papanCatur.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ini sebenernya bisa lebih clean kalau if else modulus nya didalam loop j

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jadi ga perlu for loop yang redundan (dipakai berulang) di dalam if

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overal jawaban benar, jadi lanjutkan, diatas hanya review

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@
function papanCatur(num) {
let pattern = '';
// code here
for (let i = 1; i <= num; i++) {
if (i % 2 == 1) {
for (let j = 1; j <= num; j++){
if (j % 2 == 1) {
pattern+="#";
} else {
pattern+=" ";
}
}
} else {
// harusnya gak error, yaa karena j dijalankan salah satu
// gabut hehe
for (let j = 1; j <= num; j++) {
if (j % 2 == 1) {
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 = 1; i <= num; i++) {
for (let j = 1; 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 = 1; i <= num; i++) {
for (let j = 1; j <= num; j++){
pattern+="*";
}
pattern+='\n';
}
return pattern;
}

Expand Down
16 changes: 16 additions & 0 deletions Pattern-Printing/persegiBolong.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ini juga berlaku ketimbang codenya jadi panjang karena di dalam if else ada loop, better cukup nested loop dan isinya cuma if else, lebih clean code

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
function persegiBolong(num) {
let pattern = '';
// code here
for (let i = 1; i <= num;i++) {
if (i == 1 || i==num) {
for (let j = 1;j <= num;j++) {
pattern+="*";
}
} else {
for (let k = 1; k <= num;k++) {
if (k == 1 || k == num) {
pattern+="*";
} else {
pattern+=" ";
}
}
}
pattern+="\n"
}
return pattern;
}

Expand Down
10 changes: 10 additions & 0 deletions Pattern-Printing/piramid.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
function piramida(num) {
let pattern = '';
// code here
for (let i = num; i >= 0;i--) {
for (let j = 1; j <= num * 2 - 1 - i; j++) {
if (i >= j) {
pattern+=" ";
} else {
pattern+="*";
}
}
pattern+="\n";
}
return pattern;
}

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 = 1; i <= num; i++) {
for (let j = 1; j <= num; j++) {
if (j <= num - i) {
pattern+=" ";
} else {
pattern+="* ";
}
}
pattern+="\n"
}
return pattern;
}

Expand Down
10 changes: 10 additions & 0 deletions Pattern-Printing/piramidTerbalik.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
function piramidaTerbalik(num) {
let pattern = '';
// code here
for (let i = 0; i < num ; i++) {
for (let j = 1; j <= num * 2 - i; j++) {
if (j - i <= 1) {
pattern+=" ";
} else {
pattern+="*";
}
}
pattern+="\n"
}
return pattern;
}

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 = 1; i <= num; i++) {
for (let j = 0; j < i; j++) {
pattern+="*";
}
pattern+="\n"
}
return pattern;
}

Expand Down
6 changes: 6 additions & 0 deletions Pattern-Printing/segitigaSikuTerbalik.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
function segitigaTerbalik(num) {
let pattern = '';
// code here
for (let i = 0; i < 5; i++) {
for (let j = num; j > i;j--){
pattern = pattern + "*";
}
pattern = pattern + "\n";
}
return pattern;
}

Expand Down