2. Digit*Digit

133 阅读1分钟

Welcome. in this kata, you are asked to square every digit of a number and concatenate them.

For Example,if we run 9191 throught the function, 811181 will come out , because 92 is 81 and  12 1 is 1,(811181)

Note: The function accepts an interger and returns an integer

解法1function suqareDigits(num) {
    return +(num.toString().split('').map(item => item ** 2))
}

  
解法2function squareDigits(num) {
    return +num.toString().split('').map(v => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][v]).join('')
}

  
解法3function squareDigits(num) {
    return +('' + num).replace(/\d/g, (m) => +m * +m)
}