Exercise
- Open exercise on codesandbox.
I've already imported the
ReplaySubject
class and the necessary operator functions. - Create a new ReplaySubject and specify the generic type
number
. - Use the
fromEvent
creation operator to add an event listener to both theadd
andsub
buttons. Within thepipe()
for each mouse event use themap()
operator to map to either1
or-1
appropriately. Finally, subscribe to both event listener Observables and set theReplaySubject
instance as the Observer. - Use the
EventTarget.addEventListener
to add a click event listener to thecalc
button. - In the callback function, subscribe to the
ReplaySubject
instance. Within thepipe()
use thescan()
operator to sum the next notifications values. Within thesubsribe()
method set thevalue
property for thesum
input element to the next notification value emitted.