Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created January 4, 2021 08:48
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/ef0b1964915d530cb511774c33bc6efe to your computer and use it in GitHub Desktop.
Save codecademydev/ef0b1964915d530cb511774c33bc6efe to your computer and use it in GitHub Desktop.
Codecademy export
const _ = {
clamp: function(number,lower,upper) {
let lowerClampedValue = Math.max(number,lower);
let clampedValue = Math.min(lowerClampedValue,upper);
return clampedValue;
},
inRange: function(num,start,end) {
if (end === undefined) {
end = start;
start = 0;
}
if (start > end) {
let temp = start;
start = end;
end = temp;
}
let isInRange = ((start <= num) && (num < end));
return isInRange;
},
words: function(str) {
let words = str.split(' ');
return words;
},
pad: function(string,length) {
if (string.length >= length) {
return string;
}
let startPaddingLength = Math.floor((length - string.length)/2);
let endPaddingLength = length - string.length - startPaddingLength;
let paddedString = " ".repeat(startPaddingLength) + string + " ".repeat(endPaddingLength);
return (paddedString);
},
has: function (object,key) {
hasValue = (object[key] !== undefined);
return hasValue;
},
invert: function(object) {
let invObject = {};
/*
let values = Object.values(object);
let keys = Object.keys(object);
for (i=0; i<keys.length; i++){
invObject[values[i]] = keys[i];
}
*/
for (const prop in object){
let originalValue = object[prop];
invObject[originalValue] = prop;
}
return invObject;
},
findKey: function(object,predicate){
//I need help why the code does not pass codecademy test when I activate the predicate function definition below by removing the comment.
//predicate = val => {(val !== undefined)? true:false};
for (const prop in object){
let value = object[prop];
let predicateReturnValue = predicate(value);
if (predicateReturnValue) {
return prop;
}
}
return undefined;
}
};
// 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