0
0
Rubyprogramming~10 mins

Upto and downto methods in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 5 using upto method.

Ruby
1.[1](5) { |i| puts i }
Drag options to blanks, or click blank then click option'
Atimes
Bdownto
Cupto
Deach
Attempts:
3 left
💡 Hint
Common Mistakes
Using downto instead of upto will count downwards.
Using times or each will not work directly on a number.
2fill in blank
medium

Complete the code to print numbers from 5 down to 1 using downto method.

Ruby
5.[1](1) { |i| puts i }
Drag options to blanks, or click blank then click option'
Adownto
Btimes
Ceach
Dupto
Attempts:
3 left
💡 Hint
Common Mistakes
Using upto instead of downto will count upwards.
Using times or each will not work directly on a number.
3fill in blank
hard

Fix the error in the code to print numbers from 3 up to 7.

Ruby
3.[1](7) { |num| puts num }
Drag options to blanks, or click blank then click option'
Aeach
Bupto
Cdownto
Dtimes
Attempts:
3 left
💡 Hint
Common Mistakes
Using downto will not print anything because 3 is less than 7.
Using each or times on a number is incorrect.
4fill in blank
hard

Fill both blanks to create a hash with keys from 1 to 4 and values counting down from 4 to 1.

Ruby
result = { [1].[2](1) { |i| [i, 5 - i] } }
Drag options to blanks, or click blank then click option'
A1
B4
Cdownto
Dupto
Attempts:
3 left
💡 Hint
Common Mistakes
Using upto instead of downto will count upwards.
Starting from 1 instead of 4 will reverse keys and values.
5fill in blank
hard

Fill all three blanks to create a hash with keys from 'a' to 'c' and values from 1 up to 3.

Ruby
letters = ('a'..'c').to_a
numbers = 1.[1](3).to_a
result = { [2].zip([3]).to_h }
Drag options to blanks, or click blank then click option'
Aupto
Bletters
Cnumbers
Ddownto
Attempts:
3 left
💡 Hint
Common Mistakes
Using downto instead of upto for numbers.
Mixing letters and numbers in wrong order.