Skip to content

Latest commit

 

History

History
99 lines (85 loc) · 1.73 KB

more-loops-worksheet-answers.md

File metadata and controls

99 lines (85 loc) · 1.73 KB
cars = ["old", "new", "used"]
cars.each do |car|
  puts car
end
Count car Output
1 old old
2 new new
3 used used

Answer:

old
new
used

fruits = ["banana", "apple", "kiwi"]
fruits.each do |fruit|
  puts "I love " + fruit + "!"
end
Count Fruit Output
1 banana I love banana!
2 apple I love apple!
3 kiwi I love kiwi!

Answer:

I love banana!
I love apple!
I love kiwi!

values = [8, 5, 3, 10, 14, 2]
values.each do |value|
  puts value
end
Count value Output
1 8 8
2 5 5
3 3 3
4 10 10
5 14 14
6 2 2

Answer:

8
5
3
10
14
2

total = 0
values = [4, 6, 2, 8, 11]

values.each do |value|
    total = total + value
end

puts total
Count value total total + value
1 4 0 4
2 6 4 10
3 2 10 12
4 8 12 20
5 11 20 31

| Answer:

31

values = [8, 5, 3, 10, 14, 2]
values.each do |value|
  if value == 10
    puts "Special case!"
  else
    puts "Regular values like #{value}"
  end
end
Count value value == 10 ? Output
1 8 false Regular values like 8
2 5 false Regular values like 5
3 3 false Regular values like 3
4 10 true Special case!
5 14 false Regular values like 14
6 2 false Regular values like 2

Answer:

Regular values like 8
Regular values like 5
Regular values like 3
Special case!
Regular values like 14
Regular values like 2