0
0
Rubyprogramming~20 mins

Upto and downto methods in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Upto and Downto Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of using upto method
What is the output of the following Ruby code?
Ruby
result = []
5.upto(7) { |i| result << i }
puts result.join(",")
A5,6,7
B7,6,5
C5,7
D6,7
Attempts:
2 left
💡 Hint
Remember, upto counts from the starting number up to the ending number, including both.
Predict Output
intermediate
2:00remaining
Output of using downto method
What will this Ruby code print?
Ruby
result = []
3.downto(1) { |i| result << i }
puts result.join("-")
A1-2-3
B3-2-1
C3-1
D2-1
Attempts:
2 left
💡 Hint
Downto counts down from the starting number to the ending number, including both.
🧠 Conceptual
advanced
2:00remaining
Understanding return values of upto and downto
What is the return value of the following Ruby code snippet? ```ruby result = 1.upto(3) { |i| puts i } puts result ```
A
1
2
3
nil
B
1
2
3
3
C
1
2
3
1
D
1
2
3
Enumerator
Attempts:
2 left
💡 Hint
The upto method returns the receiver after iteration.
Predict Output
advanced
2:00remaining
Output of combined upto and downto
What is the output of this Ruby code?
Ruby
output = []
2.upto(3) { |i| output << i }
3.downto(2) { |i| output << i }
puts output.join(",")
A2,3,3,2
B2,3,2,3
C3,2,2,3
D3,2,3,2
Attempts:
2 left
💡 Hint
The first loop counts up from 2 to 3, the second counts down from 3 to 2.
Predict Output
expert
2:00remaining
Counting iterations with upto and downto
How many times will the block run in this Ruby code?
Ruby
count = 0
10.downto(7) { count += 1 }
puts count
A3
B10
C7
D4
Attempts:
2 left
💡 Hint
Count how many numbers are between 10 and 7, including both.