Exercise - Pipe Function
Exercise
In this exercise we are going to create a passcode login system. The goal is to validate an entered 4-digit passcode against the known valid passcode.
- Open exercise on codesandbox.
I've already imported the necessary functions, and I have created an Observable on the
NodeList
ofbuttons
that emits when a button is clicked. - Define a new
verifyPasscode
function that returns thepipe()
function. - Within the
pipe()
function use thebufferCount()
operator to buffer the source Observable to 4 next notifications. - Use the
mergeMap()
operator to return a new Observable that is a boolean value indicating if the passcodes match. Use thesequenceEqual
operator to validate the sequence of the source Observable to an Observable of the knownPASSCODE
constant. - Add the new
verifyPasscode
operator within thepipe()
of the click event listener Observable after themap()
operator.
Bonus Exercise
As a bonus, let's take our solution from above and expand the functionality.
We would like to have the 4 span.dot
elements "filled" as the user enters their passcode, and "cleared" after 1000 milliseconds of entering the last value for the passcode.
Let's create two new operators:
fillElements()
accepts anelements
argument that is aNodeList
. Add thebg-black
class to the appropriate node based on theindex
of the next notification in the source Observable.clearElements()
accepts anelements
argument that is aNodeList
. Remove thebg-black
class from all elements after a 1000 millisecond delay.
In order to fill, and subsequently clear, a span.dot
element toggle the bg-black
class on the element.