Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Adding Conditional Logic with Filter


    Often you only want values to proceed through your stream if they meet certain criteria, just as if you were using an if statement in plain JavaScript. This lesson shows you how to use filter on your stream to only push the values that you need through your stream.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    The game will be to type in the input, the current time on the timer. If I type in zero, since the count is currently at zero, that would be a point. If I type in one, it wouldn't be a point because those aren't the same.

    When I start the timer, I'd have to type in one, or two, or three. That wouldn't be a point. That's not a point. And so on.

    The way that works is by adding a filter that can take that data object and then compare the data.count to the data.text. It will parseInt the text just to turn it into a number, then hit save.

    You can see that zero will give us a point but one won't because it didn't log out anything. As soon as I start the timer, when I hit one, then type two, then type three. I missed it. Try six now, seven or eight. I got eight. 10.

    I scored points for 10, 8, 6, 2, and 1.