0
0
Angularframework~20 mins

filter operator for selection in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RxJS Filter 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 Angular component using filter operator?

Consider this Angular component using RxJS filter operator to select values:

import { Component, signal } from '@angular/core';
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';

@Component({
  selector: 'app-filter-demo',
  standalone: true,
  template: `<div>Filtered Values: {{ filteredValues() }}</div>`
})
export class FilterDemoComponent {
  filteredValues = signal([]);

  constructor() {
    const source$ = from(['apple', 'banana', 'cherry', 'date']);
    source$.pipe(
      filter(fruit => fruit.length > 5)
    ).subscribe(value => {
      this.filteredValues.update(arr => [...arr, value]);
    });
  }
}

What will be displayed inside the <div> after component initialization?

Angular
import { Component, signal } from '@angular/core';
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';

@Component({
  selector: 'app-filter-demo',
  standalone: true,
  template: `<div>Filtered Values: {{ filteredValues() }}</div>`
})
export class FilterDemoComponent {
  filteredValues = signal<string[]>([]);

  constructor() {
    const source$ = from(['apple', 'banana', 'cherry', 'date']);
    source$.pipe(
      filter(fruit => fruit.length > 5)
    ).subscribe(value => {
      this.filteredValues.update(arr => [...arr, value]);
    });
  }
}
AFiltered Values: banana
BFiltered Values: apple,banana,cherry,date
CFiltered Values: apple,date
DFiltered Values: banana,cherry
Attempts:
2 left
💡 Hint

Think about which fruits have names longer than 5 characters.

📝 Syntax
intermediate
1:30remaining
Which option correctly uses the filter operator in Angular with RxJS?

Choose the correct syntax to filter emitted values from an observable numbers$ to only even numbers.

Angular
import { filter } from 'rxjs/operators';

numbers$.pipe(
  // filter condition here
).subscribe(console.log);
Afilter(n => n % 2 === 0)
Bfilter(n => n / 2 === 0)
Cfilter(n => n % 2 = 0)
Dfilter(n => n & 2 === 0)
Attempts:
2 left
💡 Hint

Remember the modulo operator % returns the remainder.

🔧 Debug
advanced
1:30remaining
Why does this Angular RxJS filter code not emit any values?

Look at this code snippet:

import { from } from 'rxjs';
import { filter } from 'rxjs/operators';

const source$ = from([1, 2, 3, 4]);
source$.pipe(
  filter(x => x > 5)
).subscribe(console.log);

Why does it not print anything?

Angular
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';

const source$ = from([1, 2, 3, 4]);
source$.pipe(
  filter(x => x > 5)
).subscribe(console.log);
ABecause the subscribe callback is missing.
BBecause the observable source$ is empty.
CBecause no values in the array are greater than 5, so filter excludes all.
DBecause the filter operator is not imported correctly.
Attempts:
2 left
💡 Hint

Check the condition inside the filter and the source array values.

state_output
advanced
2:00remaining
What is the final state of the signal after filtering and updating?

Given this Angular component snippet:

import { Component, signal } from '@angular/core';
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';

@Component({
  selector: 'app-signal-filter',
  standalone: true,
  template: `<div>Count: {{ count() }}</div>`
})
export class SignalFilterComponent {
  count = signal(0);

  constructor() {
    const source$ = from([10, 15, 20, 25]);
    source$.pipe(
      filter(x => x % 10 === 0)
    ).subscribe(() => {
      this.count.update(c => c + 1);
    });
  }
}

What will be the value of count() after subscription completes?

Angular
import { Component, signal } from '@angular/core';
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';

@Component({
  selector: 'app-signal-filter',
  standalone: true,
  template: `<div>Count: {{ count() }}</div>`
})
export class SignalFilterComponent {
  count = signal(0);

  constructor() {
    const source$ = from([10, 15, 20, 25]);
    source$.pipe(
      filter(x => x % 10 === 0)
    ).subscribe(() => {
      this.count.update(c => c + 1);
    });
  }
}
ACount: 4
BCount: 2
CCount: 3
DCount: 0
Attempts:
2 left
💡 Hint

Count how many numbers in the array are divisible by 10.

🧠 Conceptual
expert
1:30remaining
Which statement best describes the behavior of the filter operator in Angular RxJS streams?

Choose the most accurate description of how the filter operator works in Angular RxJS streams.

AIt selectively emits only those values from the source observable that satisfy the given condition.
BIt transforms each emitted value into a new value based on a condition.
CIt blocks all values until a condition is true, then emits all values after.
DIt accumulates values over time and emits the accumulated result when the condition is met.
Attempts:
2 left
💡 Hint

Think about whether filter changes values or just decides which to pass through.