Skip to content

Instantly share code, notes, and snippets.

@masautt
masautt / 4kl7b58.js
Created September 6, 2019 19:18
How to remove the first element from an array in JavaScript?
let arr = ["M", "A", "S", "A", "U", "T", "T"]
//Remove the first element
arr.shift(); // returns "M"
console.log(arr) // --> [ 'A', 'S', 'A', 'U', 'T', 'T' ]
@masautt
masautt / mo1dpw.js
Created September 6, 2019 19:20
How to remove the last element from an array?
let arr = ["M", "A", "S", "A", "U", "T", "T"]
//Remove the last element
arr.pop(); // returns "T"
console.log(arr) // --> [ 'M', A', 'S', 'A', 'U', 'T' ]
@masautt
masautt / pkjkh4p.js
Created September 6, 2019 19:25
How to shuffle an array in JavaScript?
function shuffleArray(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
let arr = [8,6,7,5,3,0,9];
@masautt
masautt / 30mg3o8.js
Last active September 16, 2019 04:47
How to keep track of time elapsed in JavaScript?
let start = new Date();
setTimeout(()=> {
console.log((new Date() - start) / 1000 + " seconds have passed"); // --> 5 seconds have passed
},5000);
@masautt
masautt / 5qpt0yu.js
Created September 16, 2019 04:52
How do you round numbers in JavaScript?
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
@masautt
masautt / anc4mfc.js
Created September 16, 2019 05:22
How to remove whitespace from a string in JavaScript?
// Remove all whitespace
function delWS1(str) {
return str.replace(/\s/g,"");
}
// Remove trailing and leading whitepsace
function delWS2(str) {
return str.replace(/^[ ]+|[ ]+$/g,'');
}
@masautt
masautt / settings.json
Last active November 7, 2019 21:58
VSCode Settings
{
"editor.accessibilitySupport": "off",
"editor.mouseWheelZoom": true,
"workbench.iconTheme": "material-icon-theme",
"editor.detectIndentation": false,
"explorer.confirmDelete": false,
"editor.wordWrap": "on",
"editor.minimap.enabled": false,
"terminal.integrated.rendererType": "dom",
"workbench.colorTheme": "One Dark Pro Vivid",
@masautt
masautt / 5qpt0yu.js
Created November 7, 2019 22:37
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
{
"total" : 0,
"lastUpdated" : "Thu, 07 Nov 2019 22:55:15 GMT"
}
x y
20 20
25 25
30 20
20 5
30 5