[英]How would I rewrite this code using a for loop instead of using the […Array].map() method?
Here is the code, it uses map to go through and do the function.这是代码,它使用 map 到 go 并执行 function。 However if I want to make it do a loop using for (let i = 0) how would I go about that?但是,如果我想使用 for (let i = 0) 让它做一个循环,我会怎么做 go 呢?
function randomArrayGenerator(n, low, high) {
var randoms = [...Array(n)].map(() => Math.random() * (high - low) + low);
return randoms;
}
console.log(randomArrayGenerator(10, 23, 51));
By this way通过这种方式
function randomArrayGenerator(n, low, high) { let randoms = []; for(let i = 0; i < n; i++) { randoms.push(Math.random() * (high - low) + low); } return randoms; } console.log(randomArrayGenerator(10, 23, 51));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.