Bird
0
0

You want to create a custom pipe that formats a number to show 'Even' if the number is even, 'Odd' if odd, and 'Zero' if zero. Which transform method implementation is correct?

hard📝 state output Q15 of 15
Angular - Pipes
You want to create a custom pipe that formats a number to show 'Even' if the number is even, 'Odd' if odd, and 'Zero' if zero. Which transform method implementation is correct?
Atransform(value: number): string { return value % 2 === 0 ? 'Even' : 'Odd'; }
Btransform(value: number): string { if (value === 0) return 'Zero'; return value % 2 === 0 ? 'Even' : 'Odd'; }
Ctransform(value: number): string { if (value === 0) return 'Odd'; else return 'Even'; }
Dtransform(value: number): string { if (value) return 'Even'; else return 'Zero'; }
Step-by-Step Solution
Solution:
  1. Step 1: Handle zero case explicitly

    Zero is a special case and must return 'Zero'.
  2. Step 2: Check even or odd for other numbers

    Use modulo operator to check if number is even or odd and return accordingly.
  3. Final Answer:

    transform(value: number): string { if (value === 0) return 'Zero'; return value % 2 === 0 ? 'Even' : 'Odd'; } -> Option B
  4. Quick Check:

    Zero check first, then even/odd [OK]
Quick Trick: Check zero before even/odd to avoid wrong output [OK]
Common Mistakes:
  • Not handling zero separately
  • Returning wrong labels for zero
  • Using incorrect conditions for even/odd

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes