Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to reject numbers less than 5 from the array.
Ruby
numbers = [1, 4, 6, 8] filtered = numbers.reject { |n| n [1] 5 } puts filtered
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < causes wrong filtering.
Using == removes only numbers equal to 5.
✗ Incorrect
The reject method removes elements for which the block returns true. To reject numbers less than 5, use <.
2fill in blank
mediumComplete the code to reject words longer than 3 characters.
Ruby
words = ['cat', 'dog', 'elephant', 'bat'] short_words = words.reject { |word| word.length [1] 3 } puts short_words
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > keeps the wrong words.
Using <= rejects words with length 3 or less.
✗ Incorrect
We want to reject words with length greater than 3, so use >.
3fill in blank
hardFix the error in the code to reject even numbers.
Ruby
numbers = [2, 3, 4, 5] odd_numbers = numbers.reject { |n| n [1] 2 == 0 } puts odd_numbers
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using / instead of % causes a syntax error.
Using * or - does not check evenness.
✗ Incorrect
The modulo operator % gives the remainder. To check even numbers, use n % 2 == 0.
4fill in blank
hardFill both blanks to reject words starting with 'a' or 'e'.
Ruby
words = ['apple', 'banana', 'elephant', 'dog'] filtered = words.reject { |word| word.start_with?([1]) || word.start_with?([2]) } puts filtered
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong letters causes wrong filtering.
Not using quotes causes syntax errors.
✗ Incorrect
To reject words starting with 'a' or 'e', use 'a' and 'e' in start_with?.
5fill in blank
hardFill all three blanks to reject numbers divisible by 3 or less than 4.
Ruby
numbers = [1, 2, 3, 4, 5, 6] filtered = numbers.reject { |n| n [1] 3 [3] 0 || n [2] 4 } puts filtered
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators causes wrong filtering.
Mixing up comparison operators leads to errors.
✗ Incorrect
Use % to check divisibility by 3, < to check less than 4, and == to compare to zero.