-
What is an efficient way to run the command puts "hello" five times?
-
If you're looping through an array, what method should you probably use?
-
What would be the output of the following loop?
4.times do |count| puts count end
-
If you want to produce the following output:
1 2 3 4
How could you edit the following script?
4.times do |count| puts count end
-
What is wrong with the following script?
staff = ['kelsey', 'arvin', 'vince', 'dave', 'darby'] staff.times do |name| puts name end
-
What could be improved in the following script?
seasons = ['spring', 'summer', 'fall', 'winter'] seasons.each |count| puts count end
-
If you wanted to produce the following output:
hello 0 hello 1 hello 2 hello 3 hello 4
How could you edit the following script?
5.times do puts 'hello' end
-
Explain how the block variable is getting set in the following script
4.times do |block_var| puts block_var end
-
What is the difference between the block variables in the following two loops?
languages =['English', 'Spanish', 'Chinese', 'Tamil', 'Russian'] 5.times do |block_var| puts block_var end
languages =['English', 'Spanish', 'Chinese', 'Tamil', 'Russian'] languages.each do |block_var| puts block_var end
-
If you're looping through the following array using .each, what would you name block_var? Why?
colors_array = ['red', 'blue', 'green', 'orange'] colors\_array.each do |block_var| puts block_var end
-
How could you make the following code more efficient?
count = 0 5.times do puts count count += 1 end
-
Specify the state of each variable at the end of each line each time through the loop.
ex)
Loop 1: line 1: x = 3, y = 5 line 2: x = 3, y = 7 line 3: x = 3, y = 9 Loop 2: line 1: x = 4, y = 5 line 2: x = 4, y = 7 line 3: x = 4, y = 9
size = 5 count = 0 3.times do puts "Count: #{count} and Size: #{size}" count = count + 1 end
-
Will the final puts command work in the following script? Why or why not?
5.times do x = 3 puts x end puts x
-
Will the final puts command work in the following script? Why or why not?
x = 0 5.times do x = 3 puts x end puts x
-
What are the values of each variable other than item_price_array in the following loop at:
a) the end of the third line of the loop, during round 3?
b) the end of the first line of the loop, during round 4?
item\_price_array = [10, 20, 100, 50] # 11, 11/ 22, 33/ 110, 143/ 55, 143 sales_tax = 0.1 total = 0 item\_price\_array.each do |item_price| subtotal = item\_price + item\_price * sales_tax total = total + subtotal puts "Your total so far is #{total}" end
-
Create at least 3 different loops to produce the following output using colors_array
output:
red blue green orange
colors_array = ['red', 'blue', 'green', 'orange']
-
How could you refactor(improve) the following script?
staff = ['kelsey', 'arvin', 'vince', 'dave', 'darby'] count = 0 staff.each do |staff_member| puts "#{staff_member} is number #{count} in the list" count = count + 1 end
-
Specify the state of each variable at the end of each line each time through the loop. Also specify what prints to the screen every loop.
x = 0 4.times do |count| x += 1 count = count + 1 x = 4 puts count end