Bird
0
0

How can you use reject combined with map to get an array of squares of only odd numbers from nums = [1, 2, 3, 4, 5]?

hard📝 Application Q9 of 15
Ruby - Enumerable and Collection Processing
How can you use reject combined with map to get an array of squares of only odd numbers from nums = [1, 2, 3, 4, 5]?
Anums.reject(&:even?).map { |n| n * n }
Bnums.reject(&:odd?).map { |n| n * n }
Cnums.map { |n| n * n }.reject(&:odd?)
Dnums.map(&:odd?).reject { |n| n * n }
Step-by-Step Solution
Solution:
  1. Step 1: Reject even numbers

    Use reject(&:even?) to remove even numbers, keeping only odd numbers.
  2. Step 2: Map remaining numbers to their squares

    Use map { |n| n * n } to square each odd number.
  3. Final Answer:

    nums.reject(&:even?).map { |n| n * n } -> Option A
  4. Quick Check:

    Reject even, then map squares [OK]
Quick Trick: Reject even numbers first, then map squares [OK]
Common Mistakes:
  • Rejecting odd numbers instead of even
  • Mapping before rejecting
  • Using reject with map incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes