Bird
0
0

How can you filter an array of strings names to include only those starting with the letter 'J' and convert them to uppercase in one line?

hard📝 Application Q9 of 15
Ruby - Enumerable and Collection Processing
How can you filter an array of strings names to include only those starting with the letter 'J' and convert them to uppercase in one line?
Anames.map(&:upcase).select { |n| n.start_with?("J") }
Bnames.filter { |n| n.upcase.start_with?("J") }
Cnames.select { |n| n.start_with?("J") }.map(&:upcase)
Dnames.select(&:upcase).filter { |n| n.start_with?("J") }
Step-by-Step Solution
Solution:
  1. Step 1: Filter names starting with 'J'

    Use select { |n| n.start_with?("J") } to keep only names starting with 'J'.
  2. Step 2: Convert filtered names to uppercase

    Chain map(&:upcase) to convert each selected name to uppercase.
  3. Final Answer:

    names.select { |n| n.start_with?("J") }.map(&:upcase) -> Option C
  4. Quick Check:

    Filter then map for transformation [OK]
Quick Trick: Chain select then map for filter + transform [OK]
Common Mistakes:
  • Mapping before filtering changes data
  • Using upcase inside select block incorrectly
  • Confusing select and filter usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes