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
BehaviorSubjectclass 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
BehaviorSubjectand set the value property for thesuminput to the next notification value from the BehaviorSubject. - Use the
fromEvent()operator to add an event listener to both theaddandsubbutton elements. - Use the
map()operator to map theMouseEventto either a positive1or-1for the add and subtract buttons respectively. - Subscribe to the event stream for both buttons and set the Observer to the
BehaviorSubjectinstance.