在 JavaScript 中将数字转换为反向数字数组
javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/16 16:12:17
给定一个非负整数,我们需要编写一个函数,该函数返回一个包含反向独立数字列表的数组。
例如:
348597 => The correct solution should be [7,9,5,8,4,3]
其代码为 −
const num = 348597;
const reverseArrify = num => {
const numArr = String(num).split('');
const reversed = [];
for(let i = numArr.length - 1; i >= 0; i--){
reversed[i] = +numArr.shift();
};
return reversed;
};
console.log(reverseArrify(num));
以下是控制台上的输出 −
[ 7, 9, 5, 8, 4, 3 ]
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

