Loading
Build a TypeScript Project From Scratch Tutorial (19 exercises)
Lesson

Write Logic to Reset Streak

In this lesson, you’ll write the logic that handles resetting the streak.

Transcript

Instructor: [0:00] We'll add three more tests to this with a prepopulated streak. We're doing similar things to the last lesson. We're making sure it should reset if not consecutive. It should not reset for the same-day login, and it should save it to localStorage. We'll run our tests and make sure that they fail. Cool. All three tests are failing.
[0:20] The first thing we'll do down here is, instead of returning undefined, we'll return none. We'll change this up here on the return type annotation, and it'll refactor this to say state. We'll come down here, and we'll essentially copy this block. We'll change this to saying none, and we'll come down here and copy this, paste it, and rename it to updatedStreak. We'll refactor that later.

[0:43] Let's see that our tests...We still have two failing. Cool. It should reset if not consecutive. It should not reset for the same day, so let's fix that one. Inside of here, we're going to copy this. What we're going to do is change this to zero. We're going to say none here. We're going to change this to reset. Here, we'll add reset as our other option.

[1:04] We're going to change that, because that should be reset, and we'll copy this again. We'll say if state is equal to none. If it's equal to none, we're going to return the streak, which is technically the same as down here, but we'll refactor it later.

[1:18] Let's see what's happening. We're failing two tests. To return the streak from local storage, expected 12/17, which is today, to be 12/12. I'm guessing that's a test error. Yes, we're calling new Date(), so that's going to change depending on when I run the tests. I'm going to copy this in, and we will use 12/12. OK.

[1:39] Now, we have one left. This is it should not reset the streak for a same-day login. It turns out the prepopulated is 12/12, so I don't need this logic. We're going to say streakUpdated. We need to update the assertion. Since we're doing the same day, the prepopulated is one. That needs to be a one.

[2:04] All of our tests are passing.