Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a BehaviorSubject with initial value 0.
Angular
import { BehaviorSubject } from 'rxjs'; const count$ = new BehaviorSubject([1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using null or undefined as initial value causes errors.
Leaving the initial value empty.
✗ Incorrect
The BehaviorSubject needs an initial value. Here, 0 is the starting number.
2fill in blank
mediumComplete the code to subscribe and log the current value from the BehaviorSubject.
Angular
count$.[1](value => console.log(value)); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like emit or listen.
Trying to access value directly without subscribing.
✗ Incorrect
To get updates from a BehaviorSubject, use subscribe.
3fill in blank
hardFix the error in updating the BehaviorSubject's value.
Angular
count$.[1](count$.value + 1);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using set or update which do not exist on BehaviorSubject.
Trying to assign value directly.
✗ Incorrect
To update a BehaviorSubject's value, use the next method.
4fill in blank
hardFill both blanks to create a simple store with BehaviorSubject and expose it as an observable.
Angular
private countSubject = new BehaviorSubject([1]); public count$ = this.countSubject.[2]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Exposing the BehaviorSubject directly instead of asObservable.
Using subscribe instead of asObservable.
✗ Incorrect
The store starts with 0 and exposes the observable using asObservable() to prevent external updates.
5fill in blank
hardFill all three blanks to implement an increment method that updates the BehaviorSubject's value.
Angular
increment() {
const current = this.countSubject.[1];
this.countSubject.[2](current [3] 1);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to subscribe inside increment method.
Using wrong operators or methods to update value.
✗ Incorrect
Get the current value with value, then call next with current plus one.