02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Adding Days

Ok we got the hours in our last video but we need to know how many days as well for this promo to really be useful.

Video Transcript

00:00With that said, when we're doing this segregated broken apart way, we're kind of repeating
00:04logic that we don't necessarily have to do.
00:07Now, if we would in retrospect rebuild this from scratch knowing that we would build our
00:11application in this way, then we might have stored our information and broke it down
00:16into smaller pieces.
00:18But in this scenario I didn't and it doesn't really matter that much because our application
00:21isn't that big and it's not very processor intensive.
00:24Alright, so next step I want to actually reset my get days and give it the right
00:29format. So first of all, let me just get rid of the formatting that we're using
00:33there, which is the formatting of our hours.
00:37And we want to give it a formatting that basically would give if there's more than
00:41one day, we'll say the amount of days plus the word days.
00:45If it's one day, you'll just say day.
00:47And if there's no days at all, it will return no or an empty string.
00:52All right. So first of all, I'm going to create a new variable and I'm going to
00:54call it output, which will be a type of string.
01:00And I'm going to create a new variable and I'm going to call that new variable
01:04H for hours, it's going to be an integer.
01:07And I'm just going to grab that value that we got back from our division when we
01:12found out how many days we have in the time that's left.
01:17OK, so now the first question that I want to ask is if our days are more than
01:22one day and if so, we want to do one thing.
01:25And what do we want to do?
01:26We want to set our output to be that H, the amount of days, right?
01:31So we want to set it as H plus hours.
01:36Now, the next step we want to have an else if here and we're going to ask if H
01:40equals one.
01:43So if our H equals one, we want to basically do the same output but get rid
01:46of the letter S, so it's not hours, but days.
01:52And we want to get rid of that.
01:53Oh God, why am I making so many mistakes?
01:56There we go.
01:59And it would be one day.
02:01So if we have more than one, we will output H, the amount of H days.
02:06If our output is equal to one, we're going to output H day.
02:11Now, if it's not this and it's not that, then our output will equal null
02:14by default, right?
02:16So now we could just return our output and it will automatically take care of
02:20the fact that if our value that came back was zero, it's not going to
02:24register and thus nothing will be entered into our output.
02:29All that's left for us is actually to test it out and see if it works.
02:32So what I want to do next is I want to go into my timeout display and add
02:38these new features that we've just added in.
02:40So if we scroll and look for our copy area, which oh, we're already in it.
02:46Now what I want to do is I want to add those are our days.
02:49Now, if I just add my days, I'll just add your two and get days.
02:53That's all fine, but we're going to have a problem.
02:56We're going to have a few problems.
02:58So let's say we want to, let's say in the beginning, we're just going to put
03:01here a comma.
03:02If we run an application, first of all, I wanted to break a line,
03:05which that's an easy problem.
03:07We could sort that out not too badly.
03:10But our second problem is in a scenario where there's nothing there,
03:13that the value that returned is nothing.
03:15It's going to add that value in anyways.
03:18All right, so let's solve those two problems at one time.
03:22First of all, what I want to do is I want to capture the data that is coming
03:25back from our output.
03:26So this is going to be our days variable, which is going to be a string.
03:31And I'm just going to capture it into our get days.
03:36Now I want to create a variable that's going to be called output again,
03:40which is going to be a string.
03:42Now what I want, I'm going to do with that output,
03:44I'm going to set it into my text field.
03:47But I'm going to change it based on if I have days or I don't have days.
03:51So first of all, I'm going to ask if I have days.
03:53So if days, that's all I really have to ask.
03:57And if days, what do I want to do?
03:58I want to first append into my output.
04:00First of all, I want to have something in my text anyways, no matter what.
04:03So I'm first going to put there nothing.
04:06And the reason we're going to see why in just a second.
04:09But because we want to make sure that we're not
04:11trying to add into something that has nothing.
04:13And although that in if days, it's the first time
04:16that we add any value that's actually relevant,
04:19so we could say to our output, output equals days.
04:23Because that's the value that came out from get days.
04:25Plus, I want to create a break line.
04:28So I'm going to create a small little break line here using
04:33that syntax, which basically tells Flash to break a line.
04:36All right, so we added the text in if we have the days.
04:39Now if we just went to our output and we just put in our output
04:43after our if statement, the rest, which is what is the rest.
04:46It's all of this to get hours, get minutes, get seconds.
04:51All that's still good.
04:53Only problem is that we're resetting and deleting everything
04:55out of our output.
04:56So we could instead just do plus equals.
04:59But if we do that and we didn't have a value here,
05:02we would get a really random answer.
05:05So let's see what would happen if we would do it that way.
05:07And then we could fix that problem by just setting our string
05:11by default to have just nothing in it.
05:14So we'll see that first of all, we're
05:15getting that null even though we wanted it to be nothing.
05:18So to fix that problem, all we have to do
05:21is just set our string to be equals to nothing.
05:24Thus, when we add from the empty string something,
05:27we're not going to get that null anymore.
05:33Now we want to test out and see if our days is working
05:35as well, so I'm going to have to jump into our app area.
05:38And in our app area, I'm going to have
05:40to add also at least 24 hours.
05:43So I'm going to times 24 and plus 3.
05:46So we're going to have one day.
05:47But let's test it out with two days first
05:49to see if we get the word days.
05:51And when the three seconds are up,
05:52if it converts the word day.
05:56So let's see if it's working.
05:57So excellent.
05:58We have two days.
06:00And we have a reset and 24 hours are left,
06:02and now we're in one day.
06:03Fantastic.
06:05And let's move on to our next item.
06:06And our next item would be, how would the conversion
06:10work from one day to just 24 hours left?
06:14And this is working perfectly.
06:16Only problem is that our copy is getting shifted up
06:18and down, and what I really would want
06:20is just keeping everything somewhat somewhere
06:22in the center.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!