Episode 13c - Things That Can Go Wrong With Loops

  1. Fix the code to avoid an infinite loop.
    # Return an array that doubles each number
    numbers = [1, 2, 3, 4]
    
    numbers.each do |number|
      numbers << number * 2
    end
    
    puts numbers
  2. The following code uses a while loop. A while loop is like an each loop, but it doesn't use an array. Instead it continues to run as long as the condition next to the while evaluates to true. Fix the code to avoid an infinite loop.
    # To escape the while loop, the user must input your favorite team as theirs
    puts "Who is your favorite team?"
    favorite_team = gets.chomp
    while favorite_team != "Arsenal"
      puts "Are you sure they're your favorite? I think you meant Arsenal. Try again."
    end
    
    puts "That's what I thought. Now you're on the right side."
  3. Fix the code to avoid an infinite loop.
    # Blast off! Count down from 10 and then blast off your rocket
    count = 10
    puts "Let's send this thing to space!"
    while count <= 10
      puts count
      count -= 1
    end
    
    puts "BLAST OFF!"