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);
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);
})
};