Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created October 31, 2018 16:00
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/f41417190eca24d18518a156c2952d8f to your computer and use it in GitHub Desktop.
Save codecademydev/f41417190eca24d18518a156c2952d8f to your computer and use it in GitHub Desktop.
Codecademy export
const _ = {
clamp(number, lower, upper) {
const lowerClampedValue = Math.max(number, lower);
const clampedValue = Math.min(lowerClampedValue, upper);
return clampedValue;
},
inRange(number, start, end) {
if(end === undefined) {
end = start;
start = 0;
};
if(start > end) {
const temp = end;
end = start;
start = temp;
};
const isInRange = start <= number && number < end;
return isInRange;
},
words(string) {
const words = string.split(' ');
return words;
},
pad(string, length) {
if(string.length >= length) {
return string;
};
const startPaddingLength = Math.floor((length - string.length) / 2);
const endPaddingLength = length - string.length - startPaddingLength;
const paddedString = (' ').repeat(startPaddingLength) + string + (' ').repeat(endPaddingLength);
return paddedString;
},
has(object, key) {
/*if(object[key]) {
return true;
} return false;*/
const hasValue = object[key];
if(hasValue === undefined) {
return false;
} return true;
},
invert(object) {
let invertedObject = {};
for (let key in object) {
const originalValue = object[key];
invertedObject = {originalValue: key}
}
return invertedObject;
}
};
// 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