Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / q2b43jv.js
Created September 6, 2019 19:00
How to find the first duplicate element of an array in JavaScript?
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
@masautt
masautt / 6x0juy.js
Created September 6, 2019 18:55
How to remove all even numbers from an array in JavaScript?
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
@masautt
masautt / 86526vu.js
Created September 6, 2019 18:53
How to remove all odd numbers from an array in JavaScript?
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
@masautt
masautt / tb57y5j.js
Created September 6, 2019 18:47
How to find the intersection of 2 arrays in JavaScript?
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
@masautt
masautt / v5jvz0e.js
Created September 6, 2019 18:43
How to find the smallest value of an array in JavaScript?
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/
@masautt
masautt / 8osyb4f.js
Created September 6, 2019 18:42
How to find the largest value of an array in JavaScript?
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/