Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a custom pipe class with the correct decorator.
Angular
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: '[1]' }) export class ExclaimPipe implements PipeTransform { transform(value: string): string { return value + '!'; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name instead of a simple pipe name string.
Leaving the name property empty.
✗ Incorrect
The @Pipe decorator requires a 'name' property which is the pipe's name used in templates. Here, 'exclaim' is the correct pipe name.
2fill in blank
mediumComplete the transform method signature to accept a number and return a string.
Angular
transform(value: [1]): string { return value.toFixed(2); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string type which does not have toFixed method.
Using boolean type which is invalid here.
✗ Incorrect
The transform method receives the input value. Here, it should be a number to use toFixed(2).
3fill in blank
hardFix the error in the pipe transform method to handle null or undefined input safely.
Angular
transform(value: string | null | undefined): string {
return value[1] + '!';
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!' which asserts non-null but can cause runtime errors if value is null.
Using '?.' which returns undefined if value is null, causing concatenation to fail.
✗ Incorrect
Using '|| ''' ensures that if value is null or undefined, it defaults to an empty string before concatenation.
4fill in blank
hardFill both blanks to create a pipe that converts a string to uppercase and appends a suffix.
Angular
transform(value: string, suffix: string = '[1]'): string { return value.[2]() + suffix; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using toLowerCase instead of toUpperCase.
Using suffix without quotes.
✗ Incorrect
The default suffix is '!!!' and the string method to convert to uppercase is toUpperCase().
5fill in blank
hardFill all three blanks to create a pipe that formats a date with a given format and locale.
Angular
transform(value: Date, format: string = '[1]', locale: string = '[2]'): string { return new Intl.DateTimeFormat([3], { dateStyle: format }).format(value); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing locale as a string literal instead of variable.
Using incorrect format strings.
✗ Incorrect
The default format is 'medium', locale is 'en-US', and the Intl.DateTimeFormat uses the locale variable.