0
0
Rubyprogramming~10 mins

Accessing elements (indexing, first, last) 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 get the first element of the array.

Ruby
arr = [10, 20, 30]
first_element = arr.[1]
Drag options to blanks, or click blank then click option'
Apop
Blast
Cfirst
Dshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using last instead of first.
Using pop which removes the last element.
2fill in blank
medium

Complete the code to get the last element of the array.

Ruby
numbers = [5, 15, 25, 35]
last_num = numbers.[1]
Drag options to blanks, or click blank then click option'
Afirst
Bpop
Cshift
Dlast
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop which removes the last element instead of just accessing it.
Using first which returns the first element.
3fill in blank
hard

Fix the error in the code to access the second element of the array.

Ruby
items = ['apple', 'banana', 'cherry']
second_item = items[[1]]
Drag options to blanks, or click blank then click option'
A1
B2
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 which is the third element.
Using 0 which is the first element.
4fill in blank
hard

Fill both blanks to create a hash with words as keys and their lengths as values, only for words longer than 3 letters.

Ruby
words = ['cat', 'lion', 'tiger', 'dog']
lengths = {word => word.[1] for word in words if word.length [2] 3}
Drag options to blanks, or click blank then click option'
Alength
Bsize
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using size which also works but is less common here.
Using < which filters shorter words.
5fill in blank
hard

Fill all three blanks to create a hash with uppercase words as keys, their original words as values, only if the word length is greater than 3.

Ruby
words = ['ant', 'bear', 'cat', 'dog']
result = {word.[1] => word.[2] for word in words if word.length [3] 3}
Drag options to blanks, or click blank then click option'
Aupcase
Bto_s
C>
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using length as a key method instead of upcase.
Using < instead of > in the condition.