Skip to content

Latest commit

 

History

History
21 lines (17 loc) · 471 Bytes

7 kyu - Square Every Digit.md

File metadata and controls

21 lines (17 loc) · 471 Bytes

Task

Welcome. In this kata, you are asked to square every digit of a number.

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

Note: The function accepts an integer and returns an integer

Solution

def square_digits num
  num.to_s.split("").map {|x| x.to_i*x.to_i}.join.to_i
end

Better solution

def square_digits num
  # code goes here
  num.to_s.chars.map{|x| x.to_i**2}.join.to_i
end