我想做的是将整数分开,例如1986或364,然后将它们添加到数组中,例如[1000,900,80,6]或[300,60,4],无论大小是多少数。
function convert(num) {
var numbers = String(num).split("");
var times = [1000, 100, 10, 1];
var converted = [];
for(var i = 0; i < numbers.length; i++){
converted.push(numbers[i] * times[times.length - numbers.length + i]);
}
return converted;
}
convert(360);
最佳答案
它将适用于任意数量的数字
function convert(num) {
var temp = num.toString();
var ans = [];
for (var i = 0; i < temp.length; i++) {
//get the ith character and multiply with correspondng powers of 10
ans.push(parseInt(temp.charAt(i)) * Math.pow(10, temp.length - i - 1));
}
return ans;
}
convert(39323680);