0

I have Ruby ask the user five times to enter a name, and want the answers to be spat out with each line to alternate with uppercase and lowercase. Below is what I have done and it prints out each name twice, one uppercase one lowercase. But I just want each line to alternate between uppercase and lowercase. I hope I'm making sense here...

Football_team = []

5.times do 
    puts "Please enter a UK football team:"
    Football_team << gets.chomp
end

Football_team.each do |Football_team|
    puts team.upcase
    puts team.downcase
end
Spidermonkey
  • 21
  • 1
  • 1

2 Answers2

2
football_team = []

5.times do 
  puts "Please enter a UK football team:"
  football_team << gets.chomp
end

football_team.each_with_index do |team, index|
  if index.even?
    puts team.upcase
  else
    puts team.downcase
  end
end

Note that you should use identifiers starting with capitals only for constants. While Football_team might be a constant, it is generally not a good idea. Also note that your loop variable was wrong.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Thanks so much! index.even? was exactly what I wanted! Just couldn't seem to find it online anywhere. Or I was just not asking the question right. I'll try to figure out why my loop variable was wrong. Thanks again. – Spidermonkey May 12 '15 at 01:41
0

You don't really need two loops. You can do it all in one. See below

football_team = []

5.times do |i|
    puts "Please enter a UK football team:"
    team = gets.chomp

    if i.even?
        football_team << team.upcase
    else
        football_team << team.downcase
    end
end

puts football_team

Alternatively, same solution but in shorthand:

football_team = []

5.times do |i|
    puts "Please enter a UK football team:"
    i.even? ? football_team << gets.chomp.upcase : football_team << gets.chomp.downcase
end

puts football_team