This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let start = new Date(); | |
setTimeout(()=> { | |
console.log((new Date() - start) / 1000 + " seconds have passed"); // --> 5 seconds have passed | |
},5000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function firstDupe(arr) { | |
return arr.findIndex((item, index) => arr.lastIndexOf(item) !== index) | |
} | |
console.log( firstDupe([8,6,7,5,3,0,9])); // --> -1 = !exist | |
console.log( firstDupe(["L", "E", "T", "T", "E", "R"])); // --> 1 = E | |
// https://stackoverflow.com/questions/39346182/javascript-how-to-find-first-duplicate-value-and-return-its-index |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function removeEvens(arr){ | |
return arr.filter(function (num) { | |
return num % 2 !== 0; | |
}); | |
} | |
console.log(removeEvens([8,6,7,5,3,0,9])); // --> [ 7, 5, 3, 9 ] | |
// https://stackoverflow.com/questions/18305431/how-to-remove-all-odd-numbers-in-an-array-using-javascript |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function removeOdds(arr){ | |
return arr.filter(function (num) { | |
return num % 2 === 0; | |
}); | |
} | |
console.log(removeOdds([8,6,7,5,3,0,9])); // --> [ 8, 6, 0 ] | |
// https://stackoverflow.com/questions/18305431/how-to-remove-all-odd-numbers-in-an-array-using-javascript |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findIntersection(arr1, arr2) { | |
return arr1.filter(value => arr2.includes(value)) | |
} | |
console.log(findIntersection(["M","A", "R", "E", "K"], ["M","A", "S", "A", "T", "T"])) // --> [ 'M', 'A' ] | |
// https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function smallestVal(arr) { | |
return Math.min(...arr) | |
} | |
console.log(smallestVal([1,9,5,4,11,3,3,4,2,])); // --> 1 | |
// https://www.jstips.co/en/javascript/calculate-the-max-min-value-from-an-array/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function largestVal(arr) { | |
return Math.max(...arr) | |
} | |
console.log(largestVal([1,9,5,4,11,3,3,4,2,])); // --> 11 | |
// https://www.jstips.co/en/javascript/calculate-the-max-min-value-from-an-array/ |