Complete the code to reject numbers less than 5 from the array.
numbers = [1, 4, 6, 8] filtered = numbers.reject { |n| n [1] 5 } puts filtered
The reject method removes elements for which the block returns true. To reject numbers less than 5, use <.
Complete the code to reject words longer than 3 characters.
words = ['cat', 'dog', 'elephant', 'bat'] short_words = words.reject { |word| word.length [1] 3 } puts short_words
We want to reject words with length greater than 3, so use >.
Fix the error in the code to reject even numbers.
numbers = [2, 3, 4, 5] odd_numbers = numbers.reject { |n| n [1] 2 == 0 } puts odd_numbers
The modulo operator % gives the remainder. To check even numbers, use n % 2 == 0.
Fill both blanks to reject words starting with 'a' or 'e'.
words = ['apple', 'banana', 'elephant', 'dog'] filtered = words.reject { |word| word.start_with?([1]) || word.start_with?([2]) } puts filtered
To reject words starting with 'a' or 'e', use 'a' and 'e' in start_with?.
Fill all three blanks to reject numbers divisible by 3 or less than 4.
numbers = [1, 2, 3, 4, 5, 6] filtered = numbers.reject { |n| n [1] 3 [3] 0 || n [2] 4 } puts filtered
Use % to check divisibility by 3, < to check less than 4, and == to compare to zero.
