Bird
0
0

Given this custom pipe code, what will be the output if the input value is "123"?

medium📝 component behavior Q13 of 15
NestJS - Pipes

Given this custom pipe code, what will be the output if the input value is "123"?

import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common';

@Injectable()
export class ParseIntPipe implements PipeTransform {
  transform(value: any) {
    const val = parseInt(value, 10);
    if (isNaN(val)) {
      throw new BadRequestException('Validation failed');
    }
    return val;
  }
}

Input: "123"

AReturns NaN
BThrows BadRequestException
CReturns string "123"
DReturns number 123
Step-by-Step Solution
Solution:
  1. Step 1: Understand parseInt behavior on string "123"

    parseInt("123", 10) converts the string "123" to the number 123.
  2. Step 2: Check the validation condition

    Since 123 is a valid number, isNaN(val) is false, so no exception is thrown and the number 123 is returned.
  3. Final Answer:

    Returns number 123 -> Option D
  4. Quick Check:

    parseInt("123") = 123 [OK]
Quick Trick: parseInt("123") returns number 123, no error [OK]
Common Mistakes:
  • Thinking it returns string instead of number
  • Assuming it throws error for numeric strings
  • Confusing NaN with valid number

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes