The following function takes in two dates and returns the number of days between them: // Calculate the difference of two dates in total days function diffDays(d1, d2) { let numDays = 0; let tv1 = d1.getTime(); // msec since 1970 let tv2 = d2.getTime(); numDays = (tv2 – tv1) / 1000 / 86400; numDays … Continue reading JavaScript: How to calculate the number of days between two dates
Math.round()
JavaScript: How to get a random index from an array
Let’s say I have an array of 100 integers that looks like this: let hundred = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, … Continue reading JavaScript: How to get a random index from an array
JavaScript: How to generate an array of random numbers
Let’s say I want to create a list of 10 numbers that are between the values of 0 and 1. This can be done with a simple for loop. For example: let length = 10; let max = 1; let randArray = []; for (i=0; i < length; i++) { randArray.push(Math.random(max)); } console.log(randArray); Setting the … Continue reading JavaScript: How to generate an array of random numbers