Colin Sygiel
Colin Sygiel

Reputation: 947

Why is my join() method not working?

I want to write a function that takes in a multi-digit number and returns a number that contains the result of each of those numbers squared.

I can successfully turn the number into a string and square each number using the map methods, however when I try to join the array back into a single number, it is failing.

Here's my code:

function squareDigits(num){
  let numArray = num.toString().split("").map(Number);
  return numArray.map(function(number){
    return Math.pow(number, 2).join('');
  });
}
squareDigits(52);

Upvotes: 0

Views: 111

Answers (2)

Daniel A. White
Daniel A. White

Reputation: 190941

Math.pow returns a Number, not an array, but Array.prototype.map does. join is a Array.prototype method.

Upvotes: 0

Rick Lee
Rick Lee

Reputation: 763

Try this. your .join() mis-placed

function squareDigits(num){
  let numArray = num.toString().split("").map(Number);
  return numArray.map(function(number){
    return Math.pow(number, 2)
  }).join('');
}

Upvotes: 2

Related Questions