0
question for Sunday vibes
Given a non-negative integer, return an array or a list of the individual digits in order. Specification digitize(n) separate multiple digit numbers into an array Parameters n: Number - Number to be converted Return Value Array<Number> - Array of separated single digit integers Examples n Return Value 123 [1,2,3] 8675309 [8,6,7,5,3,0,9]
2 ответов
+ 1
ohwo oghenekaro just do:
const digitize = (n) => {
r = n.toString().split('');
r.forEach((el, i, a) => { a[i] = parseInt(el); })
return r
}
digitize(123) // [1, 2, 3]
+ 1
ohwo oghenekaro,
In additional to Ulisses Cruz's answer:
It is maybe not required but here is also an snippet that removes all duplicates👍
var n = Number(1234554321),
rep = n.toString().split(""),
len = rep.length,
arr = [];
for (let i=0; i<len; i++){
if (arr.indexOf(rep[i]) == -1){
arr.push(rep[i]);
}
}
console.log(arr);