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
const randomNumberInRange = (min = 0, max = 100) => Math.floor(Math.random() * (max - min + 1)) + min; | |
randomNumberInRange() | |
// Result: Default random number range is 0 - 100, so you get a number between 0 and 100. | |
randomNumberInRange(100, 200) | |
// Result: You will get a random number between 100 and 200, where 100 is min range and 200 is max range. |
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
const isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf()); | |
isDateValid("December 17, 1995 03:24:00"); | |
// Result: true |
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
const removeDuplicates = (arr) => [...new Set(arr)]; | |
removeDuplicates([31, 56, 12, 31, 45, 12, 31]); | |
//[ 31, 56, 12, 45 ] |
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
const randomBoolean = () => Math.random() >= 0.5; | |
console.log(randomBoolean()); | |
// Result: a 50/50 change on returning true of false |
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
const dayOfYear = (date) => | |
Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24); | |
dayOfYear(new Date()); | |
// Result: 272 |
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
const touchSupported = () => { | |
('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch); | |
} | |
console.log(touchSupported()); | |
// Result: will return true if touch events are supported, false if not |
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
const average = arr => arr.reduce((a, b) => a + b) / arr.length; | |
average([21, 56, 23, 122, 67]) | |
//57.8 |
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
const getParameters = (URL) => JSON.parse('{"' + decodeURI(URL.split("?")[1]).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') +'"}'); | |
getParameters("https://www.google.de/search?q=cars&start=40"); | |
// Result: { q: 'cars', start: '40' } |
NewerOlder