All right, in this lesson, we've got kind of a two part where we've got includes and ternary operator
and we want to put them together within the challenge.
So includes as a method that determines whether an array includes a certain value amongst its entries
returning true or false as appropriate.
And so this is perfect for ternary operator because we can check to see if this boolean value is true
or false and then apply a different outcome depending on what that is.
So ternary is short form for a conditional operator.
It allows you to do it within one statement.
It expects takes in the three different operands.
And the first one is going to be the Bulleen response to the condition.
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
another output.
And they've got an example of how the territory will work over here and especially within a function.
This is really useful because it's going to be returning back.
If is a member, then it's going to be two bucks.
And if it's not a member, then it's going to be ten dollars.
So sending in either true false or one is going to have different results being returned back.
And in this case, this is where we can use that includes because this will return back either true
or false within the statement, let's create a temporary array and we could just add in some random
values in here.
We're going to use includes so we indicate what array and then use that includes method in order to
determine what value we're looking for that we want to include.
So we want to see if that array includes five.
Let's refresh.
We see that the outcome is true.
Also, let's look for six.
We also have true.
What if it's 64?
We get a result back of false.
So it doesn't include that.
So you get a boolean response.
And similar to what we were looking at, index of within arrays as well that we can check to see if
it includes set and if it doesn't if it's not within the array, then we got a negative one back.
Otherwise we got the index value.
And in this case, this is how we can return a bullet value.
Let's create one other quick function.
So wrap it within rounded brackets and we're checking to see if the value is true.
And if it is true, then we're going to return back.
Yes.
And separate it with the colon.
Otherwise we're going to return back so we can try that that function.
So passing in a boolean value.
So, yes, another boolean value of false returns back.
No.
So we can combine these together now.
So instead of looking to see that includes we can check to see if that value that we're passing in is
in the array and if it is then we're going to get includes, otherwise we're not.
So if we passed in five, we see five is there, 66 is no and so on.
So we can pass in any of the values and if it's in the array then we get return back.
Yes.
Otherwise we get return back.
No, if it's not, if it's not within the array.
So this lesson challenge, we're going to use what we just learned and take that and apply that to generate
50 random numbers from one to twenty.
And we're going to exclude these values from that list.
So 10, 15, seven one, four, two and five are not included within the response of that random number.
So using a function and output the results into the console.
So within their console, you're going to see a listing of all the numbers, but you can't return back
the ones that are within the list and you can use recursion within the function in order to accomplish
this as well.
I'll give you a quick sneak peek at the code that we're going to be using and pause the video here and
I'll walk you through how you can set this up coming up.
Okay.
So have you had an opportunity?
So first thing that we need is we need to have an array.
So this is our exclude numbers array and these are all the numbers that we're excluding.
And they can be any numbers.
I'm just picking some at random.
We're also going to need a function that's going to generate random number and passing in a minimum
and a maximum.
So it's completely flexible and dynamic.
First, that random number Samarth floor.
So using the math object on at random in order to get a random number, no other way, and then selecting
from that random, we're taking the max minus the men plus one.
And we're also going to add in the one to that.
And for now, let's return back to number and we do need an equal sign in there.
So just save that.
So now we can try that or generate random and then our minimum can be one and the maximum is four.
So every time we post that, you see we can get a random number from one to four.
We saw that we can generate any one of those numbers at random using the formula.
So the next thing that we want it to do is have 20 iterations of that.
So setting that up, setting X is equal to zero.
And we're going to just do a typical Falu and then.
Increment X so that we have a way out of the loop and then we can console log whatever number is being
returned back from that formula and we're going to have our minimum value.
So setting the minimum at 1:00 and setting the max is equal to 15.
And this is where we're going to use that formula where we've got generating random.
So you could also just hardcoded in.
But this makes it a little bit more flexible, of course, to see now that we're generating all of these
random numbers anywhere from one to 15.
So it could be any of them.
Every time we refresh, you see we've got another set of numbers there.
So there's one there's 15.
There's no numbers that are outside of that spectrum.
So that part is working great.
And the next thing that we need to do is exclude numbers.
So instead of just returning the number, we're going to do something a little bit more complex where
we want to get the result using the X numbers and then using includes and checking to see if that number
is there.
So that means that if that one of these numbers is one of the ones that we potentially might be returning,
we don't want to return it quite yet.
And what we want to do instead is we want to generate a new number.
So we're passing again into that generate random so creating recursion within the function where we're
going to generate another random number until we are able to return one that isn't within the array.
Otherwise we can just return back the number we can refresh.
And now you can see that we're excluding all of those numbers.
So no matter how many times we run it, we're never going to see one to four or five, seven, 10 or
15 within the returned numbers.
And you can shorten this as well as just doing a simple return here so you don't have to use the other
variable.
So it's going to do the same thing.
And before we conclude, the challenge was any number from one to 20.
So that's why it's good that we did set up the variables because we can update our Macs really easily.
And now you can see that we're getting any number from one to 20, but we're excluding the ones that
are within the array.