1
00:00:00,240 --> 00:00:05,550
All right, in this lesson, we've got kind of a two part where we've got includes and ternary operator

2
00:00:05,670 --> 00:00:07,710
and we want to put them together within the challenge.

3
00:00:07,950 --> 00:00:13,260
So includes as a method that determines whether an array includes a certain value amongst its entries

4
00:00:13,590 --> 00:00:16,560
returning true or false as appropriate.

5
00:00:16,770 --> 00:00:23,670
And so this is perfect for ternary operator because we can check to see if this boolean value is true

6
00:00:23,670 --> 00:00:29,100
or false and then apply a different outcome depending on what that is.

7
00:00:29,220 --> 00:00:32,950
So ternary is short form for a conditional operator.

8
00:00:32,950 --> 00:00:34,770
It allows you to do it within one statement.

9
00:00:34,960 --> 00:00:38,070
It expects takes in the three different operands.

10
00:00:38,310 --> 00:00:42,570
And the first one is going to be the Bulleen response to the condition.

11
00:00:42,780 --> 00:00:47,670
And then if it's true, it's going to give you one output and if it's false, it's going to give you

12
00:00:47,670 --> 00:00:48,420
another output.

13
00:00:48,720 --> 00:00:54,440
And they've got an example of how the territory will work over here and especially within a function.

14
00:00:54,480 --> 00:00:57,330
This is really useful because it's going to be returning back.

15
00:00:57,450 --> 00:00:59,970
If is a member, then it's going to be two bucks.

16
00:01:00,180 --> 00:01:03,720
And if it's not a member, then it's going to be ten dollars.

17
00:01:04,320 --> 00:01:11,100
So sending in either true false or one is going to have different results being returned back.

18
00:01:11,340 --> 00:01:17,070
And in this case, this is where we can use that includes because this will return back either true

19
00:01:17,340 --> 00:01:23,880
or false within the statement, let's create a temporary array and we could just add in some random

20
00:01:23,880 --> 00:01:24,870
values in here.

21
00:01:25,260 --> 00:01:32,220
We're going to use includes so we indicate what array and then use that includes method in order to

22
00:01:32,220 --> 00:01:36,240
determine what value we're looking for that we want to include.

23
00:01:36,360 --> 00:01:39,360
So we want to see if that array includes five.

24
00:01:39,750 --> 00:01:40,530
Let's refresh.

25
00:01:40,640 --> 00:01:41,990
We see that the outcome is true.

26
00:01:42,420 --> 00:01:44,250
Also, let's look for six.

27
00:01:44,250 --> 00:01:45,210
We also have true.

28
00:01:45,420 --> 00:01:47,160
What if it's 64?

29
00:01:47,340 --> 00:01:48,930
We get a result back of false.

30
00:01:48,960 --> 00:01:49,910
So it doesn't include that.

31
00:01:50,340 --> 00:01:51,900
So you get a boolean response.

32
00:01:52,110 --> 00:01:59,520
And similar to what we were looking at, index of within arrays as well that we can check to see if

33
00:01:59,520 --> 00:02:04,550
it includes set and if it doesn't if it's not within the array, then we got a negative one back.

34
00:02:04,560 --> 00:02:06,690
Otherwise we got the index value.

35
00:02:06,840 --> 00:02:09,480
And in this case, this is how we can return a bullet value.

36
00:02:09,630 --> 00:02:11,480
Let's create one other quick function.

37
00:02:11,520 --> 00:02:16,770
So wrap it within rounded brackets and we're checking to see if the value is true.

38
00:02:16,860 --> 00:02:19,920
And if it is true, then we're going to return back.

39
00:02:19,920 --> 00:02:20,370
Yes.

40
00:02:20,370 --> 00:02:21,910
And separate it with the colon.

41
00:02:21,960 --> 00:02:26,220
Otherwise we're going to return back so we can try that that function.

42
00:02:26,580 --> 00:02:28,200
So passing in a boolean value.

43
00:02:28,210 --> 00:02:31,470
So, yes, another boolean value of false returns back.

44
00:02:31,470 --> 00:02:31,830
No.

45
00:02:32,040 --> 00:02:33,960
So we can combine these together now.

46
00:02:34,230 --> 00:02:42,330
So instead of looking to see that includes we can check to see if that value that we're passing in is

47
00:02:42,330 --> 00:02:46,400
in the array and if it is then we're going to get includes, otherwise we're not.

48
00:02:46,650 --> 00:02:52,430
So if we passed in five, we see five is there, 66 is no and so on.

49
00:02:52,470 --> 00:02:57,030
So we can pass in any of the values and if it's in the array then we get return back.

50
00:02:57,420 --> 00:02:58,010
Yes.

51
00:02:58,020 --> 00:02:59,520
Otherwise we get return back.

52
00:02:59,520 --> 00:03:01,650
No, if it's not, if it's not within the array.

53
00:03:01,680 --> 00:03:07,440
So this lesson challenge, we're going to use what we just learned and take that and apply that to generate

54
00:03:07,440 --> 00:03:10,800
50 random numbers from one to twenty.

55
00:03:11,010 --> 00:03:14,910
And we're going to exclude these values from that list.

56
00:03:14,910 --> 00:03:21,990
So 10, 15, seven one, four, two and five are not included within the response of that random number.

57
00:03:22,170 --> 00:03:25,640
So using a function and output the results into the console.

58
00:03:25,890 --> 00:03:31,110
So within their console, you're going to see a listing of all the numbers, but you can't return back

59
00:03:31,110 --> 00:03:36,450
the ones that are within the list and you can use recursion within the function in order to accomplish

60
00:03:36,450 --> 00:03:37,140
this as well.

61
00:03:37,320 --> 00:03:42,810
I'll give you a quick sneak peek at the code that we're going to be using and pause the video here and

62
00:03:42,810 --> 00:03:45,810
I'll walk you through how you can set this up coming up.

63
00:03:45,960 --> 00:03:46,100
Okay.

64
00:03:46,200 --> 00:03:47,520
So have you had an opportunity?

65
00:03:47,550 --> 00:03:50,370
So first thing that we need is we need to have an array.

66
00:03:50,580 --> 00:03:56,370
So this is our exclude numbers array and these are all the numbers that we're excluding.

67
00:03:56,550 --> 00:03:58,230
And they can be any numbers.

68
00:03:58,230 --> 00:03:59,840
I'm just picking some at random.

69
00:04:00,060 --> 00:04:06,180
We're also going to need a function that's going to generate random number and passing in a minimum

70
00:04:06,180 --> 00:04:07,210
and a maximum.

71
00:04:07,230 --> 00:04:09,330
So it's completely flexible and dynamic.

72
00:04:09,330 --> 00:04:13,320
First, that random number Samarth floor.

73
00:04:13,350 --> 00:04:19,200
So using the math object on at random in order to get a random number, no other way, and then selecting

74
00:04:19,200 --> 00:04:24,750
from that random, we're taking the max minus the men plus one.

75
00:04:24,750 --> 00:04:27,180
And we're also going to add in the one to that.

76
00:04:27,270 --> 00:04:31,170
And for now, let's return back to number and we do need an equal sign in there.

77
00:04:31,290 --> 00:04:32,070
So just save that.

78
00:04:32,610 --> 00:04:40,060
So now we can try that or generate random and then our minimum can be one and the maximum is four.

79
00:04:40,110 --> 00:04:45,140
So every time we post that, you see we can get a random number from one to four.

80
00:04:45,180 --> 00:04:49,540
We saw that we can generate any one of those numbers at random using the formula.

81
00:04:49,860 --> 00:04:53,670
So the next thing that we want it to do is have 20 iterations of that.

82
00:04:54,060 --> 00:04:57,210
So setting that up, setting X is equal to zero.

83
00:04:57,480 --> 00:04:59,910
And we're going to just do a typical Falu and then.

84
00:04:59,950 --> 00:05:05,860
Increment X so that we have a way out of the loop and then we can console log whatever number is being

85
00:05:05,860 --> 00:05:10,010
returned back from that formula and we're going to have our minimum value.

86
00:05:10,060 --> 00:05:15,470
So setting the minimum at 1:00 and setting the max is equal to 15.

87
00:05:15,490 --> 00:05:19,410
And this is where we're going to use that formula where we've got generating random.

88
00:05:19,600 --> 00:05:21,730
So you could also just hardcoded in.

89
00:05:21,730 --> 00:05:26,140
But this makes it a little bit more flexible, of course, to see now that we're generating all of these

90
00:05:26,140 --> 00:05:29,370
random numbers anywhere from one to 15.

91
00:05:29,380 --> 00:05:30,460
So it could be any of them.

92
00:05:30,490 --> 00:05:33,490
Every time we refresh, you see we've got another set of numbers there.

93
00:05:33,640 --> 00:05:35,140
So there's one there's 15.

94
00:05:35,260 --> 00:05:37,590
There's no numbers that are outside of that spectrum.

95
00:05:37,900 --> 00:05:39,520
So that part is working great.

96
00:05:39,790 --> 00:05:42,850
And the next thing that we need to do is exclude numbers.

97
00:05:43,120 --> 00:05:47,530
So instead of just returning the number, we're going to do something a little bit more complex where

98
00:05:47,530 --> 00:05:55,420
we want to get the result using the X numbers and then using includes and checking to see if that number

99
00:05:55,420 --> 00:05:56,240
is there.

100
00:05:56,740 --> 00:06:03,040
So that means that if that one of these numbers is one of the ones that we potentially might be returning,

101
00:06:03,190 --> 00:06:04,920
we don't want to return it quite yet.

102
00:06:05,260 --> 00:06:08,380
And what we want to do instead is we want to generate a new number.

103
00:06:08,860 --> 00:06:14,980
So we're passing again into that generate random so creating recursion within the function where we're

104
00:06:14,980 --> 00:06:22,540
going to generate another random number until we are able to return one that isn't within the array.

105
00:06:22,540 --> 00:06:26,500
Otherwise we can just return back the number we can refresh.

106
00:06:26,770 --> 00:06:29,960
And now you can see that we're excluding all of those numbers.

107
00:06:29,980 --> 00:06:36,430
So no matter how many times we run it, we're never going to see one to four or five, seven, 10 or

108
00:06:36,430 --> 00:06:38,650
15 within the returned numbers.

109
00:06:38,890 --> 00:06:44,290
And you can shorten this as well as just doing a simple return here so you don't have to use the other

110
00:06:44,740 --> 00:06:45,340
variable.

111
00:06:45,560 --> 00:06:46,660
So it's going to do the same thing.

112
00:06:46,840 --> 00:06:51,040
And before we conclude, the challenge was any number from one to 20.

113
00:06:51,250 --> 00:06:56,130
So that's why it's good that we did set up the variables because we can update our Macs really easily.

114
00:06:56,140 --> 00:07:00,820
And now you can see that we're getting any number from one to 20, but we're excluding the ones that

115
00:07:00,820 --> 00:07:02,200
are within the array.
