0
0
Rubyprogramming~10 mins

Reject for inverse filtering 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 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'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < causes wrong filtering.
Using == removes only numbers equal to 5.
2fill in blank
medium

Complete 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'
A<=
B<
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > keeps the wrong words.
Using <= rejects words with length 3 or less.
3fill in blank
hard

Fix 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'
A-
B*
C%
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using / instead of % causes a syntax error.
Using * or - does not check evenness.
4fill in blank
hard

Fill 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'
A'a'
B'b'
C'e'
D'd'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong letters causes wrong filtering.
Not using quotes causes syntax errors.
5fill in blank
hard

Fill 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'
A%
B<
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators causes wrong filtering.
Mixing up comparison operators leads to errors.