1
00:00:00,300 --> 00:00:05,790
This lesson is going to be about JavaScript function recursion, and what it does is it gives us a quick

2
00:00:05,790 --> 00:00:08,600
ability to create a quick loop of content.

3
00:00:08,920 --> 00:00:14,880
Essentially what it's doing is it's a function calling itself from within itself.

4
00:00:15,060 --> 00:00:20,070
And this is a really neat function that we can apply and have this functionality.

5
00:00:20,220 --> 00:00:23,010
In this case, what we're doing is we're creating a loop.

6
00:00:23,250 --> 00:00:24,570
So we've got a function.

7
00:00:24,570 --> 00:00:27,230
We're passing in an argument.

8
00:00:27,240 --> 00:00:34,290
So the value of this parameter is X, and we're checking to see if X is greater than or equal to 10.

9
00:00:34,620 --> 00:00:38,580
And if it is, we add return and which will end the loop.

10
00:00:38,580 --> 00:00:45,780
Otherwise we log it out into the console, thought our recursive call, which is calling itself.

11
00:00:45,990 --> 00:00:49,850
So it's taking X and it's adding one to it.

12
00:00:49,950 --> 00:00:55,740
So the next time that it comes through exhusband increment add value and it will continue to loop through

13
00:00:55,860 --> 00:00:59,280
until it meets this condition and it can finally return the function.

14
00:00:59,310 --> 00:01:06,480
So the challenge within this lesson is to create a function that asks for a guess from the user and

15
00:01:06,480 --> 00:01:12,510
uses the guests value to compare to see if the user guessed correctly and let them know if it's correct

16
00:01:12,510 --> 00:01:17,830
or incorrect and then call the function again until you get the correct guess.

17
00:01:18,120 --> 00:01:24,870
So I know this is probably a little bit annoying of a function, but it shows the usefulness of creating

18
00:01:24,870 --> 00:01:25,650
recursion.

19
00:01:25,980 --> 00:01:32,610
And you can also use a territory operator to provide a suggestion for the next guess if the user is

20
00:01:32,610 --> 00:01:33,060
wrong.

21
00:01:33,240 --> 00:01:40,170
So we can take a look and we can see if the number is larger or smaller and make a suggestion to the

22
00:01:40,170 --> 00:01:40,570
user.

23
00:01:40,770 --> 00:01:46,320
I'll give you a quick sneak peek at the solution and you can pause the video and I'll walk you through

24
00:01:46,320 --> 00:01:47,890
the solution coming up.

25
00:01:48,360 --> 00:01:55,530
So if you had an opportunity to try this out and this is taking code that we've done earlier and applying

26
00:01:55,530 --> 00:02:00,930
it within larger block of code, so we start out with creating a secret number.

27
00:02:01,080 --> 00:02:07,530
So this is the number that we want the user to be able to guess and declaring another variable that

28
00:02:07,530 --> 00:02:16,800
can hold the guests value so that we can guess this value multiple times and then we need to invoke

29
00:02:16,800 --> 00:02:17,340
the function.

30
00:02:17,380 --> 00:02:19,820
So you can call the function guesser.

31
00:02:20,310 --> 00:02:21,540
So create a function.

32
00:02:22,450 --> 00:02:25,420
And the function is named guesser.

33
00:02:27,990 --> 00:02:35,070
So we're going to start out with a prompt of a guest for the function so we can set guests to be equal

34
00:02:35,070 --> 00:02:41,310
to whatever the value of promptness, and we're going to ask the user to guess the number.

35
00:02:41,460 --> 00:02:47,240
So this is guessing that secret number and hopefully for them, for their sake, they do.

36
00:02:47,260 --> 00:02:53,130
Guess five correctly, because unfortunately for them, this function won't end until they do get set

37
00:02:53,130 --> 00:02:53,660
correctly.

38
00:02:54,090 --> 00:02:57,110
So take a look at the value of guests.

39
00:02:57,390 --> 00:03:02,670
And we're also going to turn that, make sure that it is a number that they are guessing.

40
00:03:02,700 --> 00:03:04,110
So guess number.

41
00:03:04,290 --> 00:03:08,400
So we make sure that it is a number by using the data type conversion.

42
00:03:09,570 --> 00:03:20,580
So converting it into a guess and then we can check to see if the value of number is equal to the secret

43
00:03:20,580 --> 00:03:20,910
number.

44
00:03:22,050 --> 00:03:26,650
And if it is, then we can output a message that they guessed correctly.

45
00:03:26,790 --> 00:03:29,850
So in the console will output a message.

46
00:03:31,350 --> 00:03:32,490
You got it.

47
00:03:32,970 --> 00:03:37,910
And then to break out of our recursion, we're going to do a return.

48
00:03:38,490 --> 00:03:43,660
And if they didn't guess correctly, then we're just launching guesser.

49
00:03:43,710 --> 00:03:45,810
So this is a function guesser.

50
00:03:45,960 --> 00:03:48,420
And what this is going to do is it's going to create a prompt.

51
00:03:48,810 --> 00:03:52,170
The prompt is going to ask our user to guess a number.

52
00:03:52,440 --> 00:04:00,390
We're going to take that response value, turn it into a number and then check to see if guess number

53
00:04:00,390 --> 00:04:01,830
is equal to the secret number.

54
00:04:02,160 --> 00:04:06,210
And if it is, then we can end the function and we can move along.

55
00:04:06,300 --> 00:04:10,580
Otherwise we're just going to keep keep the user guessing.

56
00:04:10,590 --> 00:04:13,320
So try that out and we'll guess three.

57
00:04:13,710 --> 00:04:15,150
We see it's wrong.

58
00:04:15,360 --> 00:04:16,860
Six, it's wrong.

59
00:04:17,100 --> 00:04:18,750
Seven, it's wrong.

60
00:04:18,870 --> 00:04:20,790
Five, you got it.

61
00:04:20,910 --> 00:04:22,810
And it ends the function.

62
00:04:22,920 --> 00:04:24,990
So we did have a second part to this.

63
00:04:24,990 --> 00:04:29,940
And this is if you want some bonus points, you can go ahead and add this in.

64
00:04:30,190 --> 00:04:35,730
So this is taking some conditions and giving a hint to the user for the user.

65
00:04:35,730 --> 00:04:42,660
So we know that they've got it wrong here and we can console log out, taking a look at the condition

66
00:04:43,050 --> 00:04:50,240
to see if guess number is less than the secret number and we can put that into the console.

67
00:04:51,000 --> 00:04:57,060
So if we get the wrong number, we get true or we get false.

68
00:04:57,060 --> 00:05:03,900
We get a boolean value from this statement and we can apply this into a condition, trinary condition

69
00:05:04,620 --> 00:05:06,600
where we can check to see.

70
00:05:08,100 --> 00:05:16,620
So creating a message we can check to see if the guess number is less than the secret number.

71
00:05:17,430 --> 00:05:21,180
Otherwise we're going to tell them to go lower wrong.

72
00:05:23,160 --> 00:05:25,040
And then let's add the message.

73
00:05:25,650 --> 00:05:30,420
So this will give us a hint as to which direction the gas needs to be.

74
00:05:30,720 --> 00:05:32,850
So it gets to go higher.

75
00:05:33,180 --> 00:05:37,920
Yes, eight go lower, six go lower, four go higher.

76
00:05:38,040 --> 00:05:39,630
And then five, we got it right.

77
00:05:40,110 --> 00:05:43,530
So so you can add some additional functionality into your.
