Bird
0
0

What happens if the custom pipe below receives the input "abc"?

medium📝 component behavior Q5 of 15
NestJS - Pipes

What happens if the custom pipe below receives the input "abc"?

export class ParseIntPipe implements PipeTransform {
  transform(value: any) {
    const val = parseInt(value, 10);
    if (isNaN(val)) {
      throw new Error('Invalid number');
    }
    return val;
  }
}
AReturns NaN
BReturns 0
CReturns string "abc"
DThrows an Error with message 'Invalid number'
Step-by-Step Solution
Solution:
  1. Step 1: Check parseInt result for input "abc"

    parseInt("abc", 10) returns NaN because "abc" is not a number.
  2. Step 2: Evaluate error throwing condition

    Since val is NaN, the pipe throws an Error with message 'Invalid number'.
  3. Final Answer:

    Throws an Error with message 'Invalid number' -> Option D
  4. Quick Check:

    Invalid number input = throws Error [OK]
Quick Trick: Invalid number strings cause error throw in custom pipe [OK]
Common Mistakes:
  • Expecting NaN return
  • Assuming string is returned
  • Thinking 0 is returned

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes