Skip to content

Instantly share code, notes, and snippets.

@masautt
Created November 7, 2019 22:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masautt/6ed3677e19d1dcd501436ca416fcb978 to your computer and use it in GitHub Desktop.
Save masautt/6ed3677e19d1dcd501436ca416fcb978 to your computer and use it in GitHub Desktop.
FullStackFaqs - Code Answers
function roundUp(num, prec) {
prec = Math.pow(10, prec);
return Math.ceil(num * prec) / prec;
}
function roundDown(num, prec) {
prec = Math.pow(10, prec);
return Math.floor(num * prec) / prec;
}
// Built in Math function
console.log(Math.round(10.5)) // --> 11
// Custom Math Functions for Rounding Up/Down based on Precision
console.log(roundUp(1.85, 1)) // --> 1.9
console.log(roundDown(1.85, 1)) // --> 1.8
// Remove all whitespace
function delWS1(str) {
return str.replace(/\s/g,"");
}
// Remove trailing and leading whitepsace
function delWS2(str) {
return str.replace(/^[ ]+|[ ]+$/g,'');
}
// Remove trailing and leading whitepsace with built in prototype trim
function delWS3(str) {
return str.trim();
}
console.log(delWS1("! Hello World !")) // --> "!HelloWorld!"
console.log(delWS2(" Hello World ")) // --> "Hello World"
console.log(delWS3(" Hello World ")) // --> "Hello World"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment