Adding Conditional Logic with Filter

John Lindquist
InstructorJohn Lindquist

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 4 years ago

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.

[00:00] 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.

[00:12] 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.

[00:22] 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.

[00:40] 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.

[00:57] I scored points for 10, 8, 6, 2, and 1.