0
0
Angularframework~20 mins

Parameterized pipes in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Pipes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Output of parameterized pipe with multiple parameters
Given the Angular pipe usage below, what will be the rendered output?

{{ 12345 | customFormat:'prefix':'suffix' }}

Assume the customFormat pipe adds the prefix and suffix strings around the number.
Angular
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'customFormat'})
export class CustomFormatPipe implements PipeTransform {
  transform(value: number, prefix: string, suffix: string): string {
    return `${prefix}${value}${suffix}`;
  }
}
A12345prefixsuffix
B12345
Cprefix 12345 suffix
Dprefix12345suffix
Attempts:
2 left
💡 Hint
Remember the pipe parameters are passed in order after the value.
📝 Syntax
intermediate
1:30remaining
Correct syntax for passing multiple parameters to a pipe
Which of the following Angular template expressions correctly passes two parameters to a pipe named examplePipe?
A{{ value | examplePipe:param1:param2 }}
B{{ value | examplePipe:(param1, param2) }}
C{{ value | examplePipe[param1, param2] }}
D{{ value | examplePipe:param1;param2 }}
Attempts:
2 left
💡 Hint
Angular pipes accept parameters separated by colons.
🔧 Debug
advanced
2:00remaining
Why does this parameterized pipe produce unexpected output?
Consider this pipe code:

transform(value: string, prefix?: string, suffix?: string): string {
  return prefix + value + suffix;
}

When used as {{ 'test' | myPipe }}, it produces unexpected output ('undefinedtestundefined'). Why?
AThe pipe transform method must always return a number, not string
BThe pipe must have at least one parameter, so calling without parameters is invalid
Cprefix and suffix are undefined, so concatenation includes the string 'undefined'
DAngular does not allow optional parameters in pipes
Attempts:
2 left
💡 Hint
Think about what happens when undefined is concatenated with strings.
state_output
advanced
2:00remaining
Output of pipe with parameter changing dynamically
In an Angular component template:

{{ value | repeat:times }}

where value = 'ha' and times is a component property starting at 2.

If times changes to 3, what is the new output?
Angular
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'repeat'})
export class RepeatPipe implements PipeTransform {
  transform(value: string, times: number): string {
    return value.repeat(times);
  }
}
Ahaha
Bhahaha
Cha3
Dha ha ha
Attempts:
2 left
💡 Hint
The repeat method repeats the string the given number of times without spaces.
🧠 Conceptual
expert
2:30remaining
Why use parameterized pipes instead of multiple pipes?
Which is the main advantage of using a parameterized pipe over chaining multiple pipes in Angular?
AIt reduces template complexity by handling variations inside one pipe instead of multiple pipes
BAngular does not support chaining multiple pipes in templates
CMultiple pipes cannot accept parameters, only parameterized pipes can
DParameterized pipes run faster because they are compiled differently
Attempts:
2 left
💡 Hint
Think about template readability and maintainability.