Challenge - 5 Problems
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Ruby code using
length?Look at the array and the method used. What will be printed?
Ruby
arr = [1, 2, 3, 4, 5] puts arr.length
Attempts:
2 left
💡 Hint
The
length method tells how many items are in the array.✗ Incorrect
The array has 5 numbers, so
length returns 5.❓ Predict Output
intermediate2:00remaining
What does
include? return here?Check if the array contains the number 3.
Ruby
arr = [1, 2, 3, 4, 5] puts arr.include?(3)
Attempts:
2 left
💡 Hint
The method
include? returns true if the item is found.✗ Incorrect
Since 3 is in the array,
include?(3) returns true.❓ Predict Output
advanced2:00remaining
What is the output of
flatten on a nested array?This array has arrays inside. What will
flatten do?Ruby
arr = [1, [2, 3], [4, [5, 6]]] puts arr.flatten.inspect
Attempts:
2 left
💡 Hint
The
flatten method removes all nested arrays and makes one flat array.✗ Incorrect
All nested arrays are merged into one single array with all elements.
❓ Predict Output
advanced2:00remaining
What is the length of the array after flattening?
Check the length of the array after using
flatten.Ruby
arr = [[1, 2], [3, 4], 5] flat_arr = arr.flatten puts flat_arr.length
Attempts:
2 left
💡 Hint
Flattening removes nested arrays, so count all individual elements.
✗ Incorrect
The flattened array is [1, 2, 3, 4, 5], which has length 5.
🧠 Conceptual
expert2:00remaining
Which option causes an error when calling
include??One of these arrays will cause an error when calling
include? with argument 3. Which one?Attempts:
2 left
💡 Hint
Think about what happens if the variable is not an array.
✗ Incorrect
Calling
include? on nil causes a NoMethodError because nil has no such method.