Bird
0
0

How can you use downto to print only even numbers from 8 down to 2?

hard📝 Application Q9 of 15
Ruby - Loops and Iteration
How can you use downto to print only even numbers from 8 down to 2?
A8.downto(2) { |i| puts i if i.even? }
B2.upto(8) { |i| puts i if i.even? }
C8.upto(2) { |i| puts i if i.even? }
D2.downto(8) { |i| puts i if i.even? }
Step-by-Step Solution
Solution:
  1. Step 1: Use downto to count down from 8 to 2

    8.downto(2) counts down correctly.
  2. Step 2: Filter even numbers inside block

    Using if i.even? prints only even numbers.
  3. Final Answer:

    8.downto(2) { |i| puts i if i.even? } -> Option A
  4. Quick Check:

    downto with condition filters even numbers = B [OK]
Quick Trick: Use condition inside downto block to filter values [OK]
Common Mistakes:
MISTAKES
  • Using upto to count down
  • Reversing start and end
  • Not filtering inside block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes