Files
30-seconds-of-code/snippets/number-to-array-of-digits.md
Arjun Mahishi 655584d0ed Fixed typo
2017-12-15 00:25:46 +05:30

343 B

Number to array of digits

use parseInt() to get only the integer part of the quotient. use Array.reverse() to return the array in the same order as the number.

const num2array = (n) =>{
	let arr = [];
	while (n>0) { 
		arr.push(n%10); 
		n = parseInt(n/10); 
	}
	return arr.reverse();
}

// num2array(2334) -> [2, 3, 3, 4]