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
NodeListofbuttonsthat emits when a button is clicked. - Define a new
verifyPasscodefunction 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 thesequenceEqualoperator to validate the sequence of the source Observable to an Observable of the knownPASSCODEconstant. - Add the new
verifyPasscodeoperator 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 anelementsargument that is aNodeList. Add thebg-blackclass to the appropriate node based on theindexof the next notification in the source Observable.clearElements()accepts anelementsargument that is aNodeList. Remove thebg-blackclass 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.