puts "Please enter a number and I'll give you the corresponding ordinal: "
input = gets.chomp.to_i
def ordinal(n)
array = n.to_s.split('').map(&:to_i)
ending = n % 10
if ending == 1 && array[-2] != 1
return "st"
elsif ending == 2 && array[-2] != 1
return "nd"
elsif ending == 3 && array[-2] != 1
return "rd"
else
return "th"
end
end
puts "Your ordinal is #{input}#{ordinal(input)}."
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.