给定一个非负整数,我们需要编写一个函数,该函数以相反的顺序返回包含独立数字列表的数组。
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 ]