Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created September 23, 2020 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/a562bab79c9eb78cd3503e712cc1623f to your computer and use it in GitHub Desktop.
Save codecademydev/a562bab79c9eb78cd3503e712cc1623f to your computer and use it in GitHub Desktop.
Codecademy export
const _ = {
clamp(number,lBound,hBound){
//console.log(number,lBound,hBound);
/* Check which one is the highest value, the number or the low bound. Then use that number to get the lowest(Math.min) between the result of the Math.max or the High Bound
*/
return Math.min(Math.max(number,lBound),hBound);
},
inRange(number,start,end){
if (typeof end === 'undefined'){
end = start;
start = 0;
}else if (start > end){
let help = end;
end = start;
start = help;
}
//console.log(start, end, number);
return number >= start && number < end;
},
words(string){
return string.split(' ');
},
pad(string,length){
if (length > string.length){
let bPadLength = Math.floor((length - string.length) /2);
let ePadLength = length - string.length - bPadLength;
let paddedString = ' '.repeat(bPadLength) + string + ' '.repeat(ePadLength);
return paddedString;
}else {
return string;
}
},
has(obj, key){
console.log(obj[key], obj[key] != undefined);
return obj[key] != undefined;
},
invert(obj){
const invertedObject = {};
for (const key in obj){
//console.log(key, obj[key]);
let originalValue = obj[key];
invertedObject[originalValue] = key;
// invertedObject = {originalValue : key};
};
return invertedObject;
},
findKey(obj,predicate){
for (const key in obj){
if (predicate(obj[key])){
return key;
}
}
return undefined;
},
drop(array, dropItems){
const itemsToDrop = dropItems === undefined ? 1 : dropItems ;
return array.slice(itemsToDrop);
},
dropWhile(array,predicate){
return this.drop(array, array.findIndex((element, index) => !predicate(element, index, array)));
},
chunk(array, size){
if (size === undefined){
size = 1;
}
let arrayChunks = [];
for (let i = 0; i < array.length; i = i + size){
let arrayChunk = array.slice(i, i + size);
arrayChunks.push(arrayChunk);
};
return arrayChunks;
}
};
// Do not write or modify code below this line.
module.exports = _;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment