Bird
0
0

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

medium📝 component behavior Q4 of 15
NestJS - Pipes

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

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

@Injectable()
export class ParseIntPipe implements PipeTransform {
  transform(value: any, metadata: ArgumentMetadata) {
    const val = parseInt(value, 10);
    if (isNaN(val)) {
      throw new BadRequestException('Validation failed');
    }
    return val;
  }
}
AThrows BadRequestException
BReturns NaN
CReturns string "42"
DReturns number 42
Step-by-Step Solution
Solution:
  1. Step 1: Analyze transform method logic

    The pipe tries to convert the input string "42" to an integer using parseInt.
  2. Step 2: Check for NaN and return value

    Since "42" is a valid number string, parseInt returns 42, no exception is thrown, and 42 is returned.
  3. Final Answer:

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

    Valid number string input = returns number [OK]
Quick Trick: parseInt converts valid strings to numbers, else throws error [OK]
Common Mistakes:
  • Assuming string is returned
  • Expecting exception for valid number string
  • Confusing NaN with number 42

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes