简体   繁体   中英

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. However if I want to make it do a loop using for (let i = 0) how would I go about that?

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM