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

Intergrating Into Our Mini App

Integrating the push/unshift into our small sample application.

Video Transcript

00:00We're just going to delete this or even better just for now we're just going to keep this
00:03and we're going to delete it after we create our function.
00:06So let me just F6 to make everything bigger and we could just delete the traces because
00:10we really just care about these lines and we want to make sure that we use these users
00:15so I don't forget them.
00:18So let's create here a function which would be called unshift user.
00:22An unshift user will basically accept a new user and would accept a default value to it.
00:28So first of all we need a username right so this would be username which would be a string
00:34and the second value would be true or false right it would be a boolean of is right which
00:40would be a boolean value and let's set it by default to be false making sure that when
00:45we create new users automatically by default the value of is right would be false unless
00:51it's overridden.
00:53Okay which means that we don't have to use this item thus if we call our unshift user
00:58and we just send in one variable it would then ignore this item or more accurately by default
01:04just put the value false into it.
01:07Okay now it's not returning anything and what we want to do and again CS5 just close the
01:12bracket for me so make sure that you've closed your bracket when you're open a bracket.
01:18The next thing that we want to do is we want to use our unshift so I'm just going
01:20to grab our unshift right here and we want to do two things right we want to unshift for
01:26users and we want to unshift for levels.
01:31When we unshift for users what we want to do is we want to put our new username in here
01:36and when we unshift for our level we're going to send in the is right excellent.
01:43So we created the unshift function now I could just copy this and duplicate it because
01:48our push is very similar and we could just call this push user and all we really have
01:54to change here is from unshift to push and this should do the same thing.
02:01So let's see how we could actually use this and why would we wrap this in a function as
02:04you see we're working with two variables and always will work with them together because
02:09we know they're kind of linked together.
02:11We wouldn't want to work with them manually ourselves because if we do then we might
02:14by mistake make a small change that might break everything such as if we add a new
02:19user but never update the levels if we do that all the users will get wrong right so
02:26we wouldn't want to do it that way.
02:28So let's step on and let's do this so unshift user we want to send Cyrus now the default
02:34is for new users to be false right but we don't want Cyrus to be false we're going
02:38to set a second parameter for unshift user to be true and we'll see we see that is
02:44right is set to default of false.
02:47So if we wouldn't set anything Cyrus would be false so let's let's leave Bart as false
02:55and we're unshifting the user Bart without sending anything in other words he's going
02:58to be by default false.
03:00Now let's add the users for Marge and Sarah so if we push the user Marge and let's
03:07say Marge we want to be true and let's do the same thing for Sarah but Sarah we
03:17want her to be non user.
03:21Now there's no point in tracing this out because we wouldn't see anything so what
03:23we want to do let's trace out the first position which our first position should
03:28be what exactly hopefully you said the right thing which is Bart our first user
03:32is Bart.
03:33So if we leverage our initial function that we created earlier on which was message
03:38user so I'm just going to type right here message user I want to see user zero
03:46and I want to see also the last user how am I going to get to the last user
03:49so again as we said earlier message user we're going to take a users dot length
03:56and length minus one right because our index starts at zero so we're going to
04:02trace out here levels the zero user we're going to trace out here the user
04:06in the last level let's just make sure that we know what the values we expect
04:09to get for zero we said we were expecting to receive Bart which Bart is does not
04:15have rights and we're expecting to get the last user which is Sarah which is
04:20false as well so let's ask for length minus two length minus two should give us
04:24the one before the last and the one before the last is Marge and Marge is
04:29true so we're expecting out to get Bart as the first one and Marge as the last
04:34one before last so let's run our application just make sure that we're
04:38getting the values that we expect so let's see hey Bart you don't have
04:41rights yep that's what we assumed it would send us and let's just scroll down
04:46to our user message and hello Marge you have rights
04:50bingo that's exactly what we thought would happen

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!