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
52 changes: 39 additions & 13 deletions Loop-Array/countingSheep.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,44 @@

function countSheeps(sheep) {
// TODO
let result = 0;
for (i = 0; i < sheep.length; i++) {
sheep[i] ? (result += 1) : null;
}

return result;
}

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
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
17 changes: 11 additions & 6 deletions Loop-Array/findHighest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

function findHighestInt(arr) {
//your code here

let highest = arr[0];
for (i = 0; i < arr.length; i++) {
arr[i] > highest ? (highest = arr[i]) : null;
}

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])); //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
87 changes: 82 additions & 5 deletions Loop-Array/findNeedle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,91 @@

function findNeedle(haystack) {
// your code here
for (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];
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
console.log(findNeedle(haystack_4)); //found the needle at position 5
17 changes: 11 additions & 6 deletions Loop-Array/findSmallest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

function findSmallestInt(arr) {
//your code here

let smallest = arr[0];
for (i = 0; i < arr.length; i++) {
arr[i] < smallest ? (smallest = arr[i]) : null;
}

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
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
16 changes: 12 additions & 4 deletions Loop-Array/positifSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@

function positiveSum(arr) {
//code here
let result = 0;
for (i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
result += arr[i];
}
}

return result;
}

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
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
12 changes: 9 additions & 3 deletions Loop-Array/squareSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
//tambahkan dengan pangkat 2 (n**2)
//source: https://www.codewars.com/kata/515e271a311df0350d00000f

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

return result;
}

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

function century(year) {
// Finish this :)
return;
return Math.ceil(year / 100);
}

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
console.log(century(89)); //1
10 changes: 8 additions & 2 deletions Loop-Number/summation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
//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 result = 0;
for (i = 1; i <= num; i++) {
result += i;
}

return result;
}

console.log(summation(1)); //1
console.log(summation(2)); //3
console.log(summation(8)); //36
console.log(summation(8)); //36
12 changes: 10 additions & 2 deletions Loop-String/abbrevName.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
// patrick feeney => P.F
//source: https://www.codewars.com/kata/57eadb7ecd143f4c9c0000a3

function abbrevName(name){
function abbrevName(name) {
//code here
let splited = name.split(" ");
let result = [];

for (i = 0; i < splited.length; i++) {
result.push(splited[i][0]);
}

return result.join(".");
}

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
console.log(abbrevName("David Mendieta")); //D.M
15 changes: 12 additions & 3 deletions Loop-String/noSpace.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
//hilangkan spasinya saja
//source: https://www.codewars.com/kata/57eae20f5500ad98e50002c5

function noSpace(x){
function noSpace(x) {
//code here
let splited = x.split("");
let result = [];

for (i = 0; i < splited.length; i++) {
if (splited[i] !== " ") {
result.push(splited[i]);
}
}

return result.join("");
}

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
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
26 changes: 17 additions & 9 deletions Loop-String/removeChar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
//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) {
continue;
}
result += str[i];
}

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
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
12 changes: 9 additions & 3 deletions Loop-String/stringRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
//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 = "";
for (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
console.log(repeatStr(5, "Hello")); //HelloHelloHelloHelloHello
Loading