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 output = []; | |
let number = 5678; | |
while (number) { | |
const digit = number % 10; | |
output.push(digit); | |
number = Math.floor(number / 10); | |
} |
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
A fun introduction to recursion. This answer takes a Number and returns an array of Number digits. | |
It does not convert the number to a string as an intermediate step. | |
Given n = 1234, | |
* n % 10 will return first (right-moist) digit, 4 | |
* n / 10 will return 123 with some remainder | |
* Using Math.floor we can chop the remainder off | |
* Repeating these steps, we can form the entire result | |
Now we just have to build the recursion condition, | |
* If the number is already a single digit (n < 10), return an array singleton of the digit |
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 getRandomFloat(min, max) { | |
return Math.random() * (max - min) + min; | |
} | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1) + min); | |
} | |
function getRandomBool() { | |
return Math.random() >= 0.5; |
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 radians = (degrees) => { | |
return degrees * Math.PI / 180; | |
}; |