Square Every Digit

You are asked to square every digit of a number. For example, if we run 9119 through the function, 811181 will come out.

Note: The function accepts an integer and returns an integer

Solution:

function squareDigits(num){
  var numString = num.toString(), result = [];

  for(var i = 0; i < numString.length; i++) {
    result[i] = Math.pow(numString[i], 2);
  }

  return Number(result.join(''));
}

squareDigits(9119) // 811181

results matching ""

    No results matching ""