Home > Mobile >  How to create an array with random values, multiply all its elements by 5 and move them to another a
How to create an array with random values, multiply all its elements by 5 and move them to another a

Time:07-25

let array=[];

for (let i = 0; i < 5; i  ) {

let numbers=Math.floor(Math.random()*100);

    array.push(numbers);

    let multiply=array[i]*5

}

CodePudding user response:

Just multiply them before pushing into array

let array=[];

for (let i = 0; i < 5; i  ) {

let numbers=Math.floor(Math.random()*100);

    array.push(numbers*5); //fix

    //let multiply=array[i]*5

}

CodePudding user response:

const randomValArr = 
  (size, max) => [...Array(size)].map(e => ~~(Math.random() * max));

const multiplyValArr = 
  (arr, num) => arr.map(o => o * num);

// get an array of random values
const arr1 = randomValArr(10, 50);

// get an array of all values multiplied by 5
const arr2 = multiplyValArr(arr1, 5);

[arr1, arr2].forEach(arr => console.log(JSON.stringify(arr)));

  • Related