LiveLoveApp logo

Exercise - BehaviorSubject

Exercise

The goal of this exercise is to create a simple add and subtract calculator.

  1. Open exercise on codesandbox. I've already imported the BehaviorSubject class and the necessary operator functions.
  2. Create a new BehaviorSubject, specify the generic type number, and set the seed value to 0.
  3. Use the scan() operator to sum the values emitted by the BehaviorSubject.
  4. Subscribe to the BehaviorSubject and set the value property for the sum input to the next notification value from the BehaviorSubject.
  5. Use the fromEvent() operator to add an event listener to both the add and sub button elements.
  6. Use the map() operator to map the MouseEvent to either a positive 1 or -1 for the add and subtract buttons respectively.
  7. Subscribe to the event stream for both buttons and set the Observer to the BehaviorSubject instance.