我们需要编写一个包含十进制字符串数组的JavaScript函数。该函数应返回一个整数字符串数组,该整数字符串是通过对数组的原始对应十进制值求和而获得的。
例如,如果输入数组为-
const input = ["1.00","-2.5","5.33333","8.984563"];
那么输出应该是-
const output = ["1","-2","5","8"];
为此的代码将是-
const input = ["1.00","-2.5","5.33333","8.984563"]; const roundIntegers = arr => { const res = []; arr.forEach((el, ind) => { const strNum = String(el); res[ind] = parseInt(strNum); }); return res; }; console.log(roundIntegers(input));
输出结果
控制台中的输出-
[ 1, -2, 5, 8 ]