0
0
Angularframework~20 mins

Creating custom pipes in Angular - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pipe Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this custom pipe usage?
Given the Angular pipe below and its usage in a template, what will be displayed?
Angular
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'reverse'
})
export class ReversePipe implements PipeTransform {
  transform(value: string): string {
    return value.split('').reverse().join('');
  }
}

// Template usage:
// {{ 'hello' | reverse }}
AHELLO
Bhello
Colleh
Derror: pipe not found
Attempts:
2 left
💡 Hint
Think about what the pipe does to the input string.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a pure custom pipe?
Select the correct Angular pipe definition that creates a pure pipe named 'capitalize'.
A
@Pipe({ name: 'capitalize', pure: true })
export class CapitalizePipe implements PipeTransform {
  transform(value: string): string {
    return value.charAt(0).toUpperCase() + value.slice(1);
  }
}
B
@Pipe({ name: 'capitalize', pure: 'true' })
export class CapitalizePipe implements PipeTransform {
  transform(value: string): string {
    return value.toUpperCase();
  }
}
C
@Pipe({ name: 'capitalize' })
export class CapitalizePipe {
  transform(value: string): string {
    return value.toUpperCase();
  }
}
D
@Pipe({ name: 'capitalize', pure: false })
export class CapitalizePipe implements PipeTransform {
  transform(value: string): string {
    return value.toUpperCase();
  }
}
Attempts:
2 left
💡 Hint
Pure pipes have pure: true and implement PipeTransform interface.
🔧 Debug
advanced
2:00remaining
Why does this custom pipe cause a runtime error?
Examine the pipe code below. What causes the runtime error when used in a template?
Angular
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'double' })
export class DoublePipe implements PipeTransform {
  transform(value: number): number {
    return value * 2;
  }
}

// Template usage:
// {{ '5' | double }}
AThe pipe is missing the @Injectable decorator.
BThe pipe expects a number but receives a string, causing NaN result.
CThe pipe name 'double' is reserved and cannot be used.
DThe transform method is missing a return statement.
Attempts:
2 left
💡 Hint
Check the input type and what the template passes.
state_output
advanced
2:00remaining
What is the output when using this impure pipe with changing input?
Consider the impure pipe below and its usage in a component template. What will be the displayed output after the component's 'counter' property increments?
Angular
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'addRandom', pure: false })
export class AddRandomPipe implements PipeTransform {
  transform(value: number): number {
    return value + Math.floor(Math.random() * 10);
  }
}

// Component template:
// {{ counter | addRandom }}

// Initial counter = 5
// After increment, counter = 6
AThe displayed number is always counter + 0.
BThe displayed number stays the same because pipe is pure by default.
CThe pipe throws an error because Math.random() is not allowed.
DThe displayed number changes each time counter changes because pipe is impure.
Attempts:
2 left
💡 Hint
Impure pipes run on every change detection cycle.
🧠 Conceptual
expert
2:00remaining
Which statement about Angular custom pipes is TRUE?
Select the only true statement about Angular custom pipes.
ACustom pipes must implement PipeTransform and have a transform method.
BPure pipes run on every change detection cycle regardless of input changes.
CImpure pipes run only once when the component initializes.
DPipes can modify component class properties directly.
Attempts:
2 left
💡 Hint
Think about the pipe interface and lifecycle.