logo

Square Every Digit

In this kata, you are asked to square every digit of a number and concatenate them. For example, if we run 9119 through the function, 811181 will come out, because 92 is 81 and 12 is 1. (81-1-1-81


My code:

function squareDigits(num) {
                        let output = [];
                        let numArr = num.toString().split('');
                        for (let i = 0; i < numArr.length; i++) {
                          output.push(parseInt(numArr[i]) * parseInt(numArr[i]));
                        }
                        return parseInt(output.join(''));
                      }                                      
                    


And that's the best solution among all users:

function squareDigits(num){
                        return +num.toString().split('').map(i => i*i).join('');
                      }
                    

New things I learned today: