Home > front end >  How to push to an array values from inside an arrow function
How to push to an array values from inside an arrow function

Time:10-07

I'm trying to push to an array even numbers inside an arrow function but I don't how to push it in to that array.

My code:

var arrayEvenNumbers = [];
var evenNumbers = (arrayEvenNumbers) => {
  for (i = 2; i <= 20; i  ) {
    if (i % 2 == 0) {
      arrayEvenNumbers.push(i);
    }
  }
}
console.log(evenNumbers(arrayEvenNumbers));

Really I'm a beginner and I've learned a bit, but I'm trying my best

Hope someone could help me to improve

Also I don't know if I'm sending the arguments properly And I'm not native english speaker so excuse if I can't express correctly :)

CodePudding user response:

this way

var arrayEvenNumbers = [];

const evenNumbers = arr =>
  {
  for (let i = 2; i <= 20; i  )  
    if (i % 2 == 0)  arr.push(i);
 
  return arr
  }

console.log( evenNumbers(arrayEvenNumbers) )

console.log( arrayEvenNumbers )

CodePudding user response:

evenNumbers pushes it correctly, but returns undefined. You're probably looking to do:

evenNumbers(arrayEvenNumbers);
console.log(arrayEvenNumbers);
  • Related