Challenge - 5 Problems
Observable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will be the console output after subscribing?
Consider this Angular component code snippet that subscribes to an observable emitting numbers 1 to 3. What will be printed in the console?
Angular
import { Component } from '@angular/core'; import { of } from 'rxjs'; @Component({ selector: 'app-number', template: `<p>Check console output</p>` }) export class NumberComponent { constructor() { const numbers$ = of(1, 2, 3); numbers$.subscribe(value => console.log(value)); } }
Attempts:
2 left
💡 Hint
Remember that subscribing to an observable triggers its emissions one by one.
✗ Incorrect
The 'of' operator emits each value in sequence. The subscribe callback logs each value separately, so console shows 1, then 2, then 3 on separate lines.
❓ lifecycle
intermediate1:30remaining
When should you unsubscribe from an observable in Angular?
In Angular components, when is the best time to unsubscribe from an observable to avoid memory leaks?
Attempts:
2 left
💡 Hint
Think about when the component is removed from the page.
✗ Incorrect
Unsubscribing in ngOnDestroy ensures the subscription is cleaned up when the component is destroyed, preventing memory leaks.
📝 Syntax
advanced2:00remaining
Identify the error in this subscription code
What error will this Angular code produce when subscribing to an observable?
Angular
import { Component } from '@angular/core'; import { interval } from 'rxjs'; @Component({ selector: 'app-timer', template: `<p>Timer running</p>` }) export class TimerComponent { ngOnInit() { const timer$ = interval(1000); timer$.subscribe(); } }
Attempts:
2 left
💡 Hint
Is a callback function required for subscribe?
✗ Incorrect
Calling subscribe without arguments is allowed but no action happens on emissions. No error is thrown.
🔧 Debug
advanced2:00remaining
Why does this subscription not log values?
This Angular code subscribes to an observable but nothing is logged. What is the cause?
Angular
import { Component } from '@angular/core'; import { of } from 'rxjs'; @Component({ selector: 'app-test', template: `<p>Test</p>` }) export class TestComponent { constructor() { const data$ = of(10, 20, 30); data$.subscribe(); } }
Attempts:
2 left
💡 Hint
Check what subscribe needs to do something with emitted values.
✗ Incorrect
Without a callback function, subscribe does not process emitted values, so nothing is logged.
🧠 Conceptual
expert2:30remaining
What happens if you subscribe multiple times to a cold observable?
In Angular, if you subscribe multiple times to a cold observable like 'of(1,2,3)', what is the behavior?
Attempts:
2 left
💡 Hint
Think about how cold observables behave on each subscription.
✗ Incorrect
Cold observables start a new execution for each subscription, so each subscriber receives the full sequence independently.