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

190
Views
No puedo entender por qué devuelve indefinido

Estoy tratando de agregar 2 a la matriz dada. Pero el código devuelve indefinido cuando se ejecuta. Estoy tratando de aprender ES6, no pude encontrar el problema. Necesito ayuda.

 const mapSomething = (arr) => { arr.map(n => { return n + 2; }) } // It have to return [3, 4, 5] but it returns undefined console.log(mapSomething([1, 2, 3]));
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Está registrando undefined porque la función no devuelve ningún valor.

Tienes que devolver el resultado de Array.map :

 const mapSomething = (arr) => { return arr.map(n => { return n + 2; }) } // It have to return [3, 4, 5] but it returns undefined console.log(mapSomething([1, 2, 3]));

También tenga en cuenta que la declaración de return en su función de flecha es redundante, ya que solo se evalúa una expresión:

 const mapSomething = (arr) => { return arr.map(n => n + 2) } // It have to return [3, 4, 5] but it returns undefined console.log(mapSomething([1, 2, 3]));

about 4 years ago · Juan Pablo Isaza Report

0

No tiene una declaración de return en la función, por lo que devuelve undefined de forma predeterminada.

Si una función de flecha es una sola expresión que desea devolver, no debe poner {} alrededor de ella. Eso lo convierte en un cuerpo de función normal, que requiere una declaración de return explícita (como la que tiene con n + 2 ).

 const mapSomething = (arr) => arr.map(n => n + 2); // It have to return [3, 4, 5] but it returns undefined console.log(mapSomething([1, 2, 3]));

about 4 years ago · Juan Pablo Isaza Report

0

 const mapSomething = (arr) => { const arrData = arr.map(n => { return n + 2; }) return arrData } // It have to return [3, 4, 5] but it returns undefined console.log(mapSomething([1, 2, 3])) //NOTE: you where not returning anything from the function itself

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!