DEV Community

loading...

Discussion on: Daily Challenge #260 - Subtract the Sum

Collapse
mistrikushal profile image
Kushal

Here is Ruby solution,

FRUITS = {
  1 => 'kiwi',
   ...
  100 => 'pineapple'
}

def substract_sum(num)
  if num >= 10 && num < 10000
    sum_of_digits = num.to_s.chars.map(&:to_i).inject(:+)
    if FRUITS.keys.include?(sum_of_digits)
      puts FRUITS[sum_of_digits]
    else
      puts "#{sum_of_digits} (not in the list)"  
    end
  else
    puts 'Enter valid number'  
  end
end

substract_sum(10)
substract_sum(1204)