Skip to content

Instantly share code, notes, and snippets.

@shahidcodes
Last active April 13, 2017 00:57
Show Gist options
  • Save shahidcodes/eed349aa3d0b494a2f1581dc04bfe237 to your computer and use it in GitHub Desktop.
Save shahidcodes/eed349aa3d0b494a2f1581dc04bfe237 to your computer and use it in GitHub Desktop.
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);
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]);
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"]);
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);
function slasher(arr, howMany) {
// it doesn't always pay to be first
return arr.splice(howMany, arr.length);
}
slasher([1, 2, 3], 2);
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