在每个数字处截去数字以在 JavaScript 中构造一个数组
问题
我们需要编写一个接受数字的JavaScript函数。我们的函数应该返回一个字符串数组,其中包含在每个数字处截断的数字。
示例
以下是代码-
const num = 246; const cutOffEach = (num = 1) => { const str = String(num); const res = []; let temp = ''; for(let i = 0; i < str.length; i++){ const el = str[i]; temp += el; res.push(temp); }; return res; }; console.log(cutOffEach(num));输出结果
以下是控制台输出-
[ '2', '24', '246' ]