Skip to content

Instantly share code, notes, and snippets.

View hawkridge's full-sized avatar

Dmitry Beryllo hawkridge

  • Ukraine
View GitHub Profile
@hawkridge
hawkridge / digits-from-number.js
Last active October 6, 2025 08:14
Retrieve digits from number
const output = [];
let number = 5678;
while (number) {
const digit = number % 10;
output.push(digit);
number = Math.floor(number / 10);
}
@hawkridge
hawkridge / digits-from-number.js
Last active October 6, 2025 08:17
Get digits from a number recursively
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
@hawkridge
hawkridge / random.js
Last active October 6, 2025 08:16 — forked from kerimdzhanov/random.js
Get a random number from a specified range
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;
@hawkridge
hawkridge / degrees-to-radians.js
Last active October 6, 2025 08:18
Convert degrees to radians
const radians = (degrees) => {
return degrees * Math.PI / 180;
};