0
0
Rubyprogramming~10 mins

Times method for counted repetition 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 "Hello" 3 times using the times method.

Ruby
3.[1] do
  puts "Hello"
end
Drag options to blanks, or click blank then click option'
Aeach
Bloop
Ctimes
Drepeat
Attempts:
3 left
💡 Hint
Common Mistakes
Using each which is for collections, not numbers.
Using loop which creates an infinite loop.
Using repeat which is not a Ruby method.
2fill in blank
medium

Complete the code to print numbers from 0 to 4 using the times method.

Ruby
5.[1] do |i|
  puts i
end
Drag options to blanks, or click blank then click option'
Atimes
Brepeat
Cloop
Deach
Attempts:
3 left
💡 Hint
Common Mistakes
Using each which is for collections, not integers.
Using loop which does not yield iteration count.
Using repeat which is not a Ruby method.
3fill in blank
hard

Fix the error in the code to print "Hi" 4 times using the times method.

Ruby
4.times [1]
  puts "Hi"
end
Drag options to blanks, or click blank then click option'
A{ |i| do }
Bdo
Cfor
Dwhile
Attempts:
3 left
💡 Hint
Common Mistakes
Using while or for which are different loop constructs.
Adding block parameters incorrectly.
4fill in blank
hard

Fill both blanks to create a hash with numbers as keys and their squares as values using times.

Ruby
squares = {}
5.[1] do |i|
  squares[i] = i[2]2
end
Drag options to blanks, or click blank then click option'
Atimes
B+
C**
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of exponentiation.
Using each instead of times.
5fill in blank
hard

Fill all three blanks to create a hash with uppercase letters as keys and their index as values using times.

Ruby
letters = {}
26.[1] do |i|
  letters[('A'.ord[2] i).[3]] = i
end
Drag options to blanks, or click blank then click option'
Atimes
B+
Cord
Dchr
Attempts:
3 left
💡 Hint
Common Mistakes
Using ord instead of chr to convert back to letter.
Using each instead of times.
Mixing up ord and chr.