Answers for "square every digit of a number ruby"

0

if we run 9119 through the function, 811181 will come out, because 92 is 81 and 12 is 1.

function squareDigits(num){
    return Number(('' + num).split('').map(function (val) { return val * val;}).join(''));
}
Posted by: Guest on July-06-2020
0

square every digit of a number ruby

number = 12345
number.to_s.chars.map { |num| num.to_i ** 2 }.join.to_i
# convert number to string (.to_s), convert string to array (.chars)
# .map each array element to the result of a block call. 
# .join array into string & convert back to integer.
Posted by: Guest on November-26-2020

Code answers related to "square every digit of a number ruby"

Browse Popular Code Answers by Language