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

174
Views
Can I use an anonymous function with the .map() iterator in javascript?

I am trying to get this code to return a new array using .map(). For some reason I cannot figure out how to make the anonymous function work with it. Here is my code. It just returns undefined, smh.

const numbers = [2, 7, 9, 171, 52, 33, 14]

const toSquare = num => num * num

// Write your code here:
function squareNums(numArr) {
  numArr.map(num => {
    return (num ** 2);
  })
};

const numberArray = squareNums(numbers);
console.log(numberArray);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your code is correct, it's just your squareNums function doesn't return anything. In fact, that means it returns undefined, which is what you see printed in the console.

Return the result of numArr.map instead:

function squareNums(numArr) {
-  numArr.map(num => {
+  return numArr.map(num => {
    return (num ** 2);
  })
};
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!