Exercise - BehaviorSubject
Exercise
The goal of this exercise is to create a simple add and subtract calculator.
- Open exercise on codesandbox.
I've already imported the
BehaviorSubject
class and the necessary operator functions. - Create a new
BehaviorSubject
, specify the generic typenumber
, and set the seed value to0
. - Use the
scan()
operator to sum the values emitted by theBehaviorSubject
. - Subscribe to the
BehaviorSubject
and set the value property for thesum
input to the next notification value from the BehaviorSubject. - Use the
fromEvent()
operator to add an event listener to both theadd
andsub
button elements. - Use the
map()
operator to map theMouseEvent
to either a positive1
or-1
for the add and subtract buttons respectively. - Subscribe to the event stream for both buttons and set the Observer to the
BehaviorSubject
instance.