1
00:00:00,180 --> 00:00:04,350
Hey, welcome back in this lesson, we're going to be looking at the switch statement and what the switch

2
00:00:04,350 --> 00:00:10,560
statement does that evaluates an expression so matching the expressions value to the case clauses and

3
00:00:10,560 --> 00:00:13,350
then executes the statements associated with that case.

4
00:00:13,590 --> 00:00:17,040
So let's check it out and show you some examples of that.

5
00:00:17,370 --> 00:00:20,510
So I've set up an input area as well as a button.

6
00:00:20,760 --> 00:00:24,020
So we're just ask what time is it?

7
00:00:24,450 --> 00:00:26,610
And then we've got a button.

8
00:00:26,610 --> 00:00:35,130
So let's make these interactive these fields so we'll select the input area as well as the button.

9
00:00:35,730 --> 00:00:40,290
So button document using query selector.

10
00:00:40,290 --> 00:00:43,890
Let's select the button element so we only have the one.

11
00:00:43,890 --> 00:00:52,680
So just select that first one and I'll just do another one for answer and do document and query selector

12
00:00:52,680 --> 00:00:54,690
and we'll select the input.

13
00:00:54,990 --> 00:00:59,400
And as we only have one input, it's going to be relatively easy to pick these up.

14
00:00:59,700 --> 00:01:05,700
So adding an event listener to the button so that we're going to be listening for is a click.

15
00:01:06,030 --> 00:01:08,160
Whenever it gets clicked, it's going to run a function.

16
00:01:08,610 --> 00:01:11,280
And within this function, what do we want to happen?

17
00:01:11,470 --> 00:01:14,160
We want to ask the question, so what time is that?

18
00:01:14,340 --> 00:01:18,600
And we're going to do an evaluation on the switch statements.

19
00:01:18,600 --> 00:01:20,340
We'll have the switch statement here and here.

20
00:01:20,580 --> 00:01:29,130
And for now, we'll just output whatever the value of the answer and whatever the value is that's associated

21
00:01:29,130 --> 00:01:29,800
with the answer.

22
00:01:30,570 --> 00:01:36,390
So if there is no value for the answer, then we're not going to try to provide a solution for it.

23
00:01:36,660 --> 00:01:39,840
So we need to make sure that we do have a value within the answer.

24
00:01:39,860 --> 00:01:42,420
So let's try that and put in a value.

25
00:01:42,600 --> 00:01:44,870
And so now we get the value output there.

26
00:01:45,600 --> 00:01:51,750
So we want to make sure that whatever we're outputting, whatever responses in there, that this is

27
00:01:51,750 --> 00:01:53,530
going to be an actual number that we can use.

28
00:01:53,850 --> 00:02:00,510
So let's check out then let's grab that value and we're going to check to see the type that it is.

29
00:02:03,070 --> 00:02:11,440
So will console log out type of and this will tell us the data type, so by default, within the input,

30
00:02:12,070 --> 00:02:13,990
it's going to be a string value.

31
00:02:14,320 --> 00:02:18,900
So even though we've got numbers in there, it's still going to be returned back as a string.

32
00:02:19,180 --> 00:02:22,430
So we want to do is we actually want to convert it into a number.

33
00:02:23,200 --> 00:02:32,680
So let's set up a variable and set up a brand new variable and using a number, let's convert the answer

34
00:02:32,680 --> 00:02:38,320
value into a number and then we'll also get the type of four Antz.

35
00:02:38,800 --> 00:02:41,250
So the original is always going to be a string.

36
00:02:41,740 --> 00:02:47,600
So want to check to see what we're returning back and if it's a number and what type it is.

37
00:02:47,610 --> 00:02:49,230
So let's try that one more time.

38
00:02:49,480 --> 00:02:50,520
So we've got some letters.

39
00:02:50,770 --> 00:02:57,070
So it's a string, the original ones, a string and now it's been turned into a number and string and

40
00:02:57,070 --> 00:02:57,470
numbers.

41
00:02:57,470 --> 00:03:03,730
So with no we're always turning it into a number, but it doesn't necessarily mean that it is an actual

42
00:03:03,730 --> 00:03:04,810
value of a number.

43
00:03:04,840 --> 00:03:12,490
So if we cancel out the value of ants, if it's a string, then it's actually going to be returning

44
00:03:12,490 --> 00:03:15,270
back as an end.

45
00:03:15,700 --> 00:03:21,610
So if we've got a string value, it's turned back as an and and if we have a number that's going to

46
00:03:21,610 --> 00:03:23,070
still return it back as a numbers.

47
00:03:23,070 --> 00:03:28,840
So the NUM conversion is going to either check to see if it's a number and if it's not a number, then

48
00:03:29,090 --> 00:03:30,600
turn it back as not.

49
00:03:31,090 --> 00:03:36,590
So that is a variable that we can use and value representing, not a number.

50
00:03:36,610 --> 00:03:51,640
So if none is equal to and then that means that console log not a number and just duplicate that and

51
00:03:51,640 --> 00:03:54,010
else and move that in there.

52
00:03:54,580 --> 00:03:55,660
So it's aligned.

53
00:03:55,660 --> 00:03:58,930
I'll get rid of that extra line and I'll just put.

54
00:03:58,960 --> 00:04:02,490
OK, so let's check to see what gets returned back here.

55
00:04:03,070 --> 00:04:06,730
So forty four is OK whereas.

56
00:04:07,870 --> 00:04:14,010
What we're returning back, so this is being treated as a no, Sanon is being returned back as a no.

57
00:04:14,030 --> 00:04:15,230
So we're still returning back.

58
00:04:15,250 --> 00:04:19,440
OK, so it's not checking to see if it's value of nine.

59
00:04:19,660 --> 00:04:22,750
It's just checking to see if it's not a number.

60
00:04:23,230 --> 00:04:30,580
So let's update this with no and we'll just check to see if now it's going to return that and it's still

61
00:04:30,580 --> 00:04:31,370
returning back.

62
00:04:31,390 --> 00:04:35,680
OK, so this is a problem using the no method.

63
00:04:36,160 --> 00:04:48,970
So let's use the pass integer and it's going to return it back as no value and that will give us different

64
00:04:48,970 --> 00:04:49,810
values there.

65
00:04:49,820 --> 00:04:51,290
So let's update this stuff.

66
00:04:51,940 --> 00:04:57,160
So now we're using ETR and it's been returned back as not a number.

67
00:04:57,580 --> 00:05:01,970
And now we've got a number and it's been returned back as a number of value.

68
00:05:03,040 --> 00:05:09,670
So both pass integer and no, they both are converting it into a number of the difference between pass

69
00:05:09,680 --> 00:05:19,450
integer is that if you have value and then if you have some numbers, it's going to return it back up

70
00:05:19,450 --> 00:05:25,800
to where it sees the end of the number, whereas number is going to return that back separately.

71
00:05:26,080 --> 00:05:34,570
And if we do, instead of parse integer, if we use no method in order to do the conversion and we do

72
00:05:34,870 --> 00:05:38,410
two, it's going to just return it back as.

73
00:05:39,730 --> 00:05:46,840
Not a number, so it's not going to take any of the additional values, whereas Pass Integer is going

74
00:05:46,840 --> 00:05:50,430
to return it back as two, so there's a difference there.

75
00:05:50,440 --> 00:05:56,180
And depending on what you want to use, you can use either the pass integer or the number.

76
00:05:56,680 --> 00:06:03,040
So let's use the pass integer in case that there are some other values that are entered in there.

77
00:06:03,250 --> 00:06:06,640
So we want to get any of the value, the number of values out of that.

78
00:06:07,450 --> 00:06:13,300
So now that we've got all of that and we're still not able to check to see if this is an actual number,

79
00:06:13,300 --> 00:06:15,070
if it's a valid number value.

80
00:06:15,730 --> 00:06:23,680
So let's take a number and we'll see if this works as we do need to have a number of value for the switch

81
00:06:23,680 --> 00:06:24,180
statement.

82
00:06:24,190 --> 00:06:30,880
So let's add in a number and it's coming back, not a number and it's coming back that this is OK.

83
00:06:30,910 --> 00:06:32,150
So that is OK.

84
00:06:32,170 --> 00:06:40,510
So we need to negate this and then we should be good to make sure that we're checking to see if not

85
00:06:40,510 --> 00:06:40,980
a number.

86
00:06:41,470 --> 00:06:43,420
And now we are getting a number.

87
00:06:43,430 --> 00:06:46,480
So it is OK, stay tuned for part number two.

88
00:06:46,480 --> 00:06:48,640
Coming up, where we're going to look at switch statements.
