This lesson is going to be about JavaScript function recursion, and what it does is it gives us a quick
ability to create a quick loop of content.
Essentially what it's doing is it's a function calling itself from within itself.
And this is a really neat function that we can apply and have this functionality.
In this case, what we're doing is we're creating a loop.
So we've got a function.
We're passing in an argument.
So the value of this parameter is X, and we're checking to see if X is greater than or equal to 10.
And if it is, we add return and which will end the loop.
Otherwise we log it out into the console, thought our recursive call, which is calling itself.
So it's taking X and it's adding one to it.
So the next time that it comes through exhusband increment add value and it will continue to loop through
until it meets this condition and it can finally return the function.
So the challenge within this lesson is to create a function that asks for a guess from the user and
uses the guests value to compare to see if the user guessed correctly and let them know if it's correct
or incorrect and then call the function again until you get the correct guess.
So I know this is probably a little bit annoying of a function, but it shows the usefulness of creating
recursion.
And you can also use a territory operator to provide a suggestion for the next guess if the user is
wrong.
So we can take a look and we can see if the number is larger or smaller and make a suggestion to the
user.
I'll give you a quick sneak peek at the solution and you can pause the video and I'll walk you through
the solution coming up.
So if you had an opportunity to try this out and this is taking code that we've done earlier and applying
it within larger block of code, so we start out with creating a secret number.
So this is the number that we want the user to be able to guess and declaring another variable that
can hold the guests value so that we can guess this value multiple times and then we need to invoke
the function.
So you can call the function guesser.
So create a function.
And the function is named guesser.
So we're going to start out with a prompt of a guest for the function so we can set guests to be equal
to whatever the value of promptness, and we're going to ask the user to guess the number.
So this is guessing that secret number and hopefully for them, for their sake, they do.
Guess five correctly, because unfortunately for them, this function won't end until they do get set
correctly.
So take a look at the value of guests.
And we're also going to turn that, make sure that it is a number that they are guessing.
So guess number.
So we make sure that it is a number by using the data type conversion.
So converting it into a guess and then we can check to see if the value of number is equal to the secret
number.
And if it is, then we can output a message that they guessed correctly.
So in the console will output a message.
You got it.
And then to break out of our recursion, we're going to do a return.
And if they didn't guess correctly, then we're just launching guesser.
So this is a function guesser.
And what this is going to do is it's going to create a prompt.
The prompt is going to ask our user to guess a number.
We're going to take that response value, turn it into a number and then check to see if guess number
is equal to the secret number.
And if it is, then we can end the function and we can move along.
Otherwise we're just going to keep keep the user guessing.
So try that out and we'll guess three.
We see it's wrong.
Six, it's wrong.
Seven, it's wrong.
Five, you got it.
And it ends the function.
So we did have a second part to this.
And this is if you want some bonus points, you can go ahead and add this in.
So this is taking some conditions and giving a hint to the user for the user.
So we know that they've got it wrong here and we can console log out, taking a look at the condition
to see if guess number is less than the secret number and we can put that into the console.
So if we get the wrong number, we get true or we get false.
We get a boolean value from this statement and we can apply this into a condition, trinary condition
where we can check to see.
So creating a message we can check to see if the guess number is less than the secret number.
Otherwise we're going to tell them to go lower wrong.
And then let's add the message.
So this will give us a hint as to which direction the gas needs to be.
So it gets to go higher.
Yes, eight go lower, six go lower, four go higher.
And then five, we got it right.
So so you can add some additional functionality into your.