将数字拆分为n个长度的数组-JavaScript
我们需要编写一个JavaScript函数,该函数接受两个数字,即m和n,并返回大小为n的数组,结果数组的所有元素加起来为m。
让我们为该函数编写代码-
示例
以下是代码-
const len = 8;
const sum = 5;
const splitNumber = (len, sum) => {
const res = [];
for(let i = 0; i < len; i++){
res.push(sum / len);
};
return res;
};
console.log(splitNumber(len, sum));输出结果
控制台中的输出:-
[ 0.625, 0.625, 0.625, 0.625, 0.625, 0.625, 0.625, 0.625 ]