Bird
0
0

Why does this code cause a runtime error?

medium📝 Debug Q7 of 15
Angular - RxJS Operators
Why does this code cause a runtime error?
import { of } from 'rxjs';
import { map } from 'rxjs/operators';

of(1, 2, 3).pipe(
  map(x => x.toUpperCase())
).subscribe(console.log);
ABecause subscribe() is missing error handling
BBecause map operator is used incorrectly
CBecause of() cannot emit numbers
DBecause numbers do not have a toUpperCase() method
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the emitted values and method called

    The observable emits numbers 1, 2, 3 but toUpperCase() is a string method.
  2. Step 2: Understand the runtime error cause

    Calling toUpperCase() on a number causes a runtime error because the method does not exist on numbers.
  3. Final Answer:

    Because numbers do not have a toUpperCase() method -> Option D
  4. Quick Check:

    toUpperCase only works on strings [OK]
Quick Trick: Check method compatibility with data type [OK]
Common Mistakes:
MISTAKES
  • Assuming toUpperCase works on numbers
  • Blaming map syntax
  • Ignoring runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes