1 00:00:01,010 --> 00:00:09,890 Up to this point we learned the 'if' statement, the 'if else' statement and the 'else if' statement 2 00:00:09,890 --> 00:00:17,660 And if you remember our section outline, well we haven't mentioned these two for javascript conditionals. 3 00:00:17,660 --> 00:00:19,660 So we're going to tackle those today. 4 00:00:20,060 --> 00:00:24,190 As you can see we have two more to get through but don't be scared. 5 00:00:24,290 --> 00:00:27,360 These are not going to introduce anything new. 6 00:00:27,360 --> 00:00:33,170 There are just different ways of doing control flow but they won't do anything that a simple if else 7 00:00:33,330 --> 00:00:34,560 statement can't do. 8 00:00:34,940 --> 00:00:43,790 However there are cases where it will be much easier to do these two then if we do something with the 9 00:00:43,790 --> 00:00:45,490 'if else' statement. 10 00:00:45,920 --> 00:00:47,330 So let's check them out. 11 00:00:47,360 --> 00:00:50,870 The first thing we're going to talk about is the 'ternary operator'. 12 00:00:50,870 --> 00:00:54,950 It looks something like this, 13 00:00:55,480 --> 00:00:56,150 'condition ?' 14 00:00:58,740 --> 00:01:02,310 expression one (expr1) or expression two (expr2). 15 00:01:06,230 --> 00:01:10,820 And that is the syntax. It says, Is this true or false? 16 00:01:11,000 --> 00:01:14,790 If it's true then provide this value (expr1). 17 00:01:15,170 --> 00:01:17,870 If it's false provide this value(expr2). 18 00:01:18,230 --> 00:01:27,260 Let me show you an example of this, will have a function called 'isUserValid' that accepts a boolean (bool) 19 00:01:29,570 --> 00:01:34,190 and will just return the boolean value. 20 00:01:36,190 --> 00:01:48,670 Now within here we can say variable answer equals 'isUserValid' 21 00:01:48,670 --> 00:01:50,090 and we'll say 'true' for now question mark(?) 22 00:01:50,290 --> 00:01:54,820 So if that's true I'll say 'You may enter' 23 00:01:58,300 --> 00:02:03,630 otherwise I'll say 'Access denied' 24 00:02:03,850 --> 00:02:06,600 Let me make this a little bit smaller so you can see. 25 00:02:06,820 --> 00:02:12,790 So again all we're saying is if this is true then do this expression("You may enter") 26 00:02:12,790 --> 00:02:15,960 If this is false do this expression("Access Denied"). 27 00:02:16,110 --> 00:02:19,300 So just copy this into the console and see what happens. 28 00:02:21,520 --> 00:02:27,780 If I run 'anwer', Whoops I spell that wrong, 'answer'. 29 00:02:28,790 --> 00:02:33,570 I get, 'You may enter' because 30 00:02:33,580 --> 00:02:34,600 user is valid (isUserValid) is true. 31 00:02:34,640 --> 00:02:47,030 Now if I changed this to say 'false', well in that case I'll get 'Access denied' 32 00:02:47,030 --> 00:02:49,040 Let's do a little bit more of a complicated answer. 33 00:02:49,100 --> 00:02:58,870 We can say variable (var) 'automatedAnswer' and we can say and I'll do this on a new line just so you can see 34 00:02:59,050 --> 00:03:08,880 'Your account number is', 'isUserValid' and we'll just say something like. 35 00:03:08,880 --> 00:03:10,140 'false' for now. 36 00:03:10,140 --> 00:03:16,680 So because we have these brackets we're gonna have an expression inside of here that we can evaluate 37 00:03:16,680 --> 00:03:18,990 so we can say is user (isUserValid) is valid. 38 00:03:19,020 --> 00:03:23,280 If the user is valid will say '1234'. 39 00:03:23,420 --> 00:03:28,940 If not, we'll say 'not available'. 40 00:03:31,780 --> 00:03:37,830 If we copy and paste this here, into our console and we can see it better. 41 00:03:37,870 --> 00:03:39,880 We see, if user is valid (true) 42 00:03:39,880 --> 00:03:42,610 We want to say '1234'. 43 00:03:42,640 --> 00:03:45,070 If it's not then 'not available'. 44 00:03:45,070 --> 00:03:54,490 So if I run this and now I look at automated answer, I get 'Your account number is not available' well, 45 00:03:54,700 --> 00:03:56,920 because this user value is 'false'. 46 00:03:56,920 --> 00:04:08,190 But if I do 'true' well in that case your account number is '1234'. 47 00:04:08,210 --> 00:04:14,120 Now you might notice that this is actually not that different from if you did, 48 00:04:14,120 --> 00:04:18,170 'function condition' was called a condition and will say 49 00:04:21,600 --> 00:04:27,260 'if isUserValid' return. 50 00:04:27,290 --> 00:04:28,490 'You may enter' 51 00:04:31,510 --> 00:04:33,670 and you did 'else' 52 00:04:36,450 --> 00:04:55,330 'return Access denied' this function over here, 'condition', if I say variable(var) 'answer2' equals 'condition'. 53 00:04:55,570 --> 00:04:56,790 Let's see what happens. 54 00:04:59,380 --> 00:05:09,680 Copy these two, run them, and now if I look at the 'answer' I get 'You may enter' if I get the 'answer2' 55 00:05:09,690 --> 00:05:13,120 I get 'You may enter'. 56 00:05:13,290 --> 00:05:18,280 Now it's the exact same thing other than well bit of mis-spelling on my part. 57 00:05:19,630 --> 00:05:28,710 But you see that one way is a lot nicer, It's just nice simple one line. A ternary operator is really really 58 00:05:28,710 --> 00:05:36,060 good for these 'if else' where there is a check for a condition and there's two possible expressions, 59 00:05:36,060 --> 00:05:36,790 that come out of it. 60 00:05:38,410 --> 00:05:44,350 All right let's learn, the next one, which is the 'switch' statement. 61 00:05:45,390 --> 00:05:46,290 This is the fun one. 62 00:05:47,350 --> 00:05:49,750 So a s'witch' statement looks something like this. 63 00:05:49,770 --> 00:05:53,980 And we can build a simple game right now to demonstrate how it works. 64 00:05:54,070 --> 00:05:58,720 I can say function 'moveCommand'. 65 00:05:59,350 --> 00:06:05,120 And we'll say it accepts parameter 'direction' within this function. 66 00:06:05,130 --> 00:06:12,790 I'll say variable(var) 'whatHappens' and we'll say 'switch'. 67 00:06:12,810 --> 00:06:13,850 Notice that it's a key word. 68 00:06:13,850 --> 00:06:16,140 So it's now highlighted in red. 69 00:06:16,160 --> 00:06:19,000 And I'll say 'direction'. 70 00:06:19,160 --> 00:06:23,790 The 'switch' statement is gonna accept 'direction' it's going to check 'direction'. 71 00:06:25,430 --> 00:06:37,600 And within here we'll say 'case' 'forward' which means check 'direction' if 'direction' equals 'forward', 72 00:06:37,820 --> 00:06:42,410 Then 'whatHappens' equals 73 00:06:42,900 --> 00:06:45,210 'You encounter a monster;' 74 00:06:48,490 --> 00:06:49,360 'break;'. 75 00:06:49,390 --> 00:06:54,750 I'll explain more 'break' does right after I finish up the rest of the case statements. 76 00:06:54,790 --> 00:07:01,210 So case statements as you can see are really good because I can just copy and paste. 77 00:07:01,470 --> 00:07:14,550 And now I can say for here we'll say 'back' and if direction is 'back' we'll say 'You arrived home' if the 78 00:07:14,550 --> 00:07:29,320 direction is 'right' in that case we'll say 'You found a river' and if the 'direction' is 'left' we'll say 79 00:07:30,670 --> 00:07:34,050 'You run into a troll'. 80 00:07:35,280 --> 00:07:39,930 And then finally we'll say 'default' 81 00:07:43,330 --> 00:07:49,840 'whatHappens' equals to 'please enter a valid direction'. 82 00:07:52,740 --> 00:07:58,530 And we'll end it with a 'return' 'whatHappens'. 83 00:07:58,530 --> 00:07:58,950 All right. 84 00:07:58,950 --> 00:08:01,170 That was a whole lot of code. 85 00:08:01,170 --> 00:08:06,080 Let's run this command first and then we'll explain exactly what is happening. 86 00:08:06,090 --> 00:08:07,030 Copy the code. 87 00:08:08,000 --> 00:08:10,160 Copy into the console. 88 00:08:10,160 --> 00:08:11,760 And now we have the 'moveCommand' 89 00:08:11,840 --> 00:08:21,940 And if you remember the last video 'moveCommand' now exists in the root scope which is window, 90 00:08:21,940 --> 00:08:22,300 so within here. 91 00:08:22,330 --> 00:08:31,860 Let's just say 'forward', now if I run this I get, 'You encountered a monster'. 92 00:08:32,030 --> 00:08:36,640 If I go 'back', 'You arrived home'. 93 00:08:36,640 --> 00:08:40,320 If I go 'right', 'You found a river'. 94 00:08:40,690 --> 00:08:46,830 If I go 'left', 'You run into a troll' and if I write jibberish 95 00:08:50,700 --> 00:08:51,070 I get. 96 00:08:51,090 --> 00:08:53,000 'Please enter a valid direction'. 97 00:08:53,980 --> 00:08:58,510 That's what switch statements are really good for when you have a lot of conditions. 98 00:08:58,510 --> 00:09:07,470 Instead of using an 'if else', 'if else', 'if else', 'if else', 'if else' statement, you can use a 'switch' statement 99 00:09:08,070 --> 00:09:10,710 and you can see, it reads nicely. 100 00:09:10,710 --> 00:09:16,760 Essentially what it's saying is create variable 'whatHappens' then switch. 101 00:09:16,770 --> 00:09:23,660 I want you to check whatever the condition is, whatever the variable is, in this case that's the direction. 102 00:09:23,970 --> 00:09:30,190 And in case, the direction equals 'forward' do this, in case direction is 'back' do this. 103 00:09:30,240 --> 00:09:33,810 If it's 'right' do this, if it's 'left' do this. 104 00:09:33,810 --> 00:09:42,060 And it says 'default' is if none of these conditions work and remember the program goes line by line 105 00:09:42,060 --> 00:09:46,260 so it checks 'forward' then it checks 'back' and it checks 'right' and the checks 'left'. 106 00:09:46,280 --> 00:09:53,940 And if nothing of those match then it says you can just default to 'please enter a valid direction' and 107 00:09:53,940 --> 00:09:59,700 then we return 'whatHappenes' so that was so that the function returns something for us. 108 00:09:59,710 --> 00:10:05,460 Now finally what is the 'break' well the 'break'(s) simply says, 109 00:10:06,850 --> 00:10:12,260 If 'case' is 'forward' then assign 'you encounter a monster' to 110 00:10:12,280 --> 00:10:18,620 'whatHhappens' variable and then 'break', 'break' is just saying break out of the switch statement. 111 00:10:18,640 --> 00:10:24,720 So instead of the program then checking to see if 'case' is 'back', 'case' is 'right', 'case' is 'left' or 'default'. 112 00:10:24,850 --> 00:10:33,550 If we say 'break', the program stops here and then goes straight out of the switch statement 113 00:10:33,910 --> 00:10:34,870 to return 'whatHappens'. 114 00:10:35,140 --> 00:10:46,830 So if I actually do, 'break' before 'whatHappens', 'you encounter a monster' and I copy this 115 00:10:49,680 --> 00:10:58,820 and let's rerun the function, we'll say 'moveCommand' is 'forward' notice what happens now. 116 00:10:59,840 --> 00:11:06,930 I get 'undefined' because we broke the program enters saying, Oh yeah! 117 00:11:07,320 --> 00:11:14,010 He typed in 'forward' but before we're able to assign something to the variable I say 'break', 118 00:11:14,040 --> 00:11:21,530 I exit out of the switch statement and I return 'whatHappens' which it's only been declared but it's unassigned. 119 00:11:21,530 --> 00:11:26,630 So I just return 'undefined'. 120 00:11:26,680 --> 00:11:32,530 All right there you have it, your two other conditionals and you've covered all of them in javascript. 121 00:11:32,530 --> 00:11:39,710 Now each one is good for its own individual case but they're all very useful. 122 00:11:39,760 --> 00:11:41,470 I'll see you in the next one Buh-bye.