Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
How to push to an array values from inside an arrow function

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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 )

about 4 years ago · Juan Pablo Isaza Report

0

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

evenNumbers(arrayEvenNumbers);
console.log(arrayEvenNumbers);
about 4 years ago · Juan Pablo Isaza Report

0

this is the code you've forgotten the return statement:

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!