Last active
April 13, 2017 00:57
-
-
Save shahidcodes/eed349aa3d0b494a2f1581dc04bfe237 to your computer and use it in GitHub Desktop.
FreeCodeCamp Javascript Algorithm | https://www.freecodecamp.com/map | https://www.freecodecamp.com/shahidkh4n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function chunkArrayInGroups(arr, size) { | |
// Break it up. | |
var times = arr.length / size; | |
console.log(times); | |
var f = 0, e = size; | |
var narr = []; | |
for(var i=0; i<times; i++){ | |
narr.push ( arr.slice(f, e) ); | |
f = e; | |
e += size; | |
} | |
return narr; | |
} | |
chunkArrayInGroups(["a", "b", "c", "d"], 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function bouncer(arr) { | |
// Don't show a false ID to this bouncer. | |
var filtered = arr.filter(Boolean); | |
console.log(filtered); | |
return filtered; | |
} | |
bouncer([7, "ate", "", false, 9]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mutation(arr) { | |
bPointer = false; | |
var narr = arr[1].toLowerCase().split(""); | |
for(var i=0; i<narr.length; i++){ | |
var x = narr[i]; | |
bPointer = (arr[0].toLowerCase().indexOf(x) != -1); | |
console.log(x, arr[0], bPointer); | |
if(!bPointer) break; | |
} | |
return bPointer; | |
} | |
mutation(["hello", "hey"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function repeatStringNumTimes(str, num) { | |
// repeat after me | |
String.prototype.repeat = function(n){ | |
t = ''; | |
for(var i=0; i<n; i++){ | |
t += this; | |
} | |
return t; | |
}; | |
if(num > 0) | |
return str.repeat(num); | |
else return ''; | |
} | |
repeatStringNumTimes("abc", 3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function slasher(arr, howMany) { | |
// it doesn't always pay to be first | |
return arr.splice(howMany, arr.length); | |
} | |
slasher([1, 2, 3], 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function truncateString(str, num) { | |
// Clear out that junk in your trunk | |
if( num < str.length && num > 3){ | |
return str.slice(0, num-3) + '...'; | |
}else if(num<3){ | |
return str.slice(0, num) + '...'; | |
} | |
return str; | |
} | |
truncateString("A-", 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment