Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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));
like image 226
Trickster Avatar asked Jun 28 '26 04:06

Trickster


1 Answers

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));
like image 128
sonEtLumiere Avatar answered Jun 30 '26 17:06

sonEtLumiere



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!