Reputation: 947
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
Reputation: 190941
Math.pow
returns a Number
, not an array, but Array.prototype.map
does. join
is a Array.prototype
method.
Upvotes: 0
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