1
00:00:00,210 --> 00:00:00,810
All right.

2
00:00:00,810 --> 00:00:03,900
So let's go over the solutions to these three problems.

3
00:00:03,930 --> 00:00:10,100
Let's start with the first one is even so to get started here I'm going to go ahead and open up supply

4
00:00:10,120 --> 00:00:10,430
.

5
00:00:10,740 --> 00:00:18,530
I have an index that aged him I'll file and I'm going to add a script tag and we'll just call this solution

6
00:00:18,750 --> 00:00:19,110
that.

7
00:00:19,230 --> 00:00:21,410
Yes.

8
00:00:21,420 --> 00:00:28,410
So next up I need to make this solution that J file and I'm going to save that solution.

9
00:00:28,640 --> 00:00:32,130
Yes in the same place and quite safe.

10
00:00:32,700 --> 00:00:35,340
So again we're starting with is even.

11
00:00:35,610 --> 00:00:38,430
So I'm going to declare the function is even

12
00:00:41,340 --> 00:00:43,100
and it needs to take a single argument.

13
00:00:43,200 --> 00:00:47,640
Let's just call it numb and then we want to return true

14
00:00:50,580 --> 00:00:56,990
if even return false otherwise.

15
00:00:57,030 --> 00:00:59,040
So there's a few ways of doing this.

16
00:00:59,100 --> 00:01:01,880
The first one is to do it as explicitly as possible.

17
00:01:02,070 --> 00:01:10,830
So we're going to say if numb mod 2 is zero remember that's how we tell if something is even if the

18
00:01:10,830 --> 00:01:15,430
remainder of dividing two into that number is zero if there is no remainder.

19
00:01:15,480 --> 00:01:16,680
That means that it's even.

20
00:01:16,770 --> 00:01:25,600
So if that's the case we can return TRUE else return false.

21
00:01:25,830 --> 00:01:27,230
Just like that.

22
00:01:27,870 --> 00:01:29,520
And I'm going to move this up here

23
00:01:32,820 --> 00:01:37,240
just to make it clear what corresponds to what.

24
00:01:37,470 --> 00:01:39,610
Just like that.

25
00:01:40,080 --> 00:01:41,260
So we can do this.

26
00:01:41,580 --> 00:01:44,480
And I'm actually not going to call my function just yet.

27
00:01:44,490 --> 00:01:49,430
I'm going to open it up in the browser to show you something slightly new.

28
00:01:49,860 --> 00:01:57,500
So if I open up my console I actually have access to is even in my console here.

29
00:01:57,870 --> 00:02:04,170
So I type is even with no parentheses and it tells me yes I know about is even Here's the code and if

30
00:02:04,170 --> 00:02:09,880
I add the parentheses I need to pass that number in like 5 and I get false.

31
00:02:10,440 --> 00:02:19,120
And I can do it again with four or with one or with ninety nine and it works great.

32
00:02:19,530 --> 00:02:21,860
So there's two things there are is even works.

33
00:02:21,900 --> 00:02:22,910
So that's awesome.

34
00:02:22,950 --> 00:02:28,590
But more importantly I want to show you that we can define functions and code in our files.

35
00:02:28,800 --> 00:02:31,420
And then when we open those files up in the browser.

36
00:02:31,620 --> 00:02:35,510
In this case our aged him L5 that's connected to our javascript file.

37
00:02:35,580 --> 00:02:40,140
I can then access those functions.

38
00:02:40,140 --> 00:02:42,500
So there is a small refactor that we can make here.

39
00:02:42,750 --> 00:02:49,650
I'm going to copy this code commented all out and I'm going to shorten this up so I'm just going to

40
00:02:49,650 --> 00:02:57,470
show you the solution first and then we'll go over how it works.

41
00:02:57,480 --> 00:03:00,670
So this is substantially shorter return.

42
00:03:00,800 --> 00:03:04,260
Number two equals equals equals zero.

43
00:03:04,260 --> 00:03:05,610
So let's refresh the page.

44
00:03:05,640 --> 00:03:12,980
Make sure that it works is even for 4 is true is even if 3 is false.

45
00:03:12,990 --> 00:03:15,060
Still works the same way.

46
00:03:16,260 --> 00:03:19,660
So the way that it actually works this line here.

47
00:03:19,710 --> 00:03:22,920
Return Nahm made to triple equals zero.

48
00:03:23,130 --> 00:03:28,750
It's going to evaluate this first and that's a true or false statement it's a boolean statement.

49
00:03:29,040 --> 00:03:33,180
So numb to Tripoli equals zero is either going to be true or false.

50
00:03:33,270 --> 00:03:35,840
And then we're just returning true or false.

51
00:03:36,120 --> 00:03:43,800
So rather than checking if it's true return true and if it's false return false we just turn the value

52
00:03:43,800 --> 00:03:44,970
immediately.

53
00:03:44,970 --> 00:03:48,170
So that's a nice little shortcut there.

54
00:03:49,860 --> 00:03:52,100
So next up we have factorial.

55
00:03:52,320 --> 00:04:00,510
So I'm going to go ahead and start by defining my function called factorial function factorial and we

56
00:04:00,510 --> 00:04:03,070
know need to take a number.

57
00:04:04,110 --> 00:04:05,780
So down here I'm going to write a note.

58
00:04:05,880 --> 00:04:14,430
So if I did factorial of four that should give me four times three times two times one and I'm going

59
00:04:14,430 --> 00:04:17,340
to come with that.

60
00:04:17,340 --> 00:04:20,820
So inside of our function I'm going to write a few lines of pseudo code.

61
00:04:20,850 --> 00:04:26,730
The first thing that we know we want to do is define a result variable and then the very last thing

62
00:04:26,730 --> 00:04:30,050
in the function is return the result variable.

63
00:04:30,480 --> 00:04:34,230
So in-between that's important and that's where we're going to calculate

64
00:04:36,960 --> 00:04:42,770
factorial and store value in result.

65
00:04:44,060 --> 00:04:47,740
So we have a few approaches in how we can calculate factorial.

66
00:04:47,880 --> 00:04:54,290
We could either start by setting result to be one at the very beginning and then multiplying it by two

67
00:04:54,620 --> 00:04:59,910
and then multiplying it by three and then by 4 or we could go the other way around like we have here

68
00:04:59,920 --> 00:05:00,120
.

69
00:05:00,390 --> 00:05:08,140
And I'm going to start by doing it this way so you need to a result variable var result equals 1.

70
00:05:08,730 --> 00:05:15,710
And then what we need to do is multiply that result by every number between 1 and that number.

71
00:05:15,750 --> 00:05:20,110
So one in four or one in ten whatever is passed in as No.

72
00:05:20,670 --> 00:05:25,840
So to do that we'll want to use a loop we could use either a for loop or a while loop.

73
00:05:26,040 --> 00:05:28,800
I'm going to use a for loop here because it's a little bit shorter.

74
00:05:28,920 --> 00:05:32,750
So for far and let's just call it I.

75
00:05:33,300 --> 00:05:35,060
And we could start I as one.

76
00:05:35,160 --> 00:05:37,030
So I'll do that first.

77
00:05:37,410 --> 00:05:45,330
And we're going to keep multiplying while I is less than or equal to numb then we're going to add 1

78
00:05:45,480 --> 00:05:47,380
to i every time.

79
00:05:47,880 --> 00:05:54,950
And then all that we want to do is multiply result by I.

80
00:05:55,250 --> 00:06:04,080
So if we step through this if we did it for four factorial of four result starts as one right here then

81
00:06:04,120 --> 00:06:09,250
we're creating I start says 1 and we multiply result by.

82
00:06:09,450 --> 00:06:13,350
So that's just 1 times 1 which I noticed is unnecessary.

83
00:06:13,350 --> 00:06:16,410
So we might want to just start this at 2.

84
00:06:16,440 --> 00:06:20,350
So the first time through we multiply by times result.

85
00:06:20,400 --> 00:06:22,560
So we get 1 times 2.

86
00:06:22,680 --> 00:06:27,710
Next time through I is now three because we added one.

87
00:06:27,990 --> 00:06:30,920
So that is still less than the number which is four.

88
00:06:30,930 --> 00:06:38,040
So we multiply again result equals the current value a result which is this times 3.

89
00:06:39,090 --> 00:06:43,990
So then we have 6 and then the next time through I now is four.

90
00:06:44,000 --> 00:06:46,250
So four is less than or equal to four.

91
00:06:46,250 --> 00:06:47,230
That is true.

92
00:06:47,580 --> 00:06:53,790
So we're going to multiply one last time by four and then add one to I in our four loop and now we're

93
00:06:53,790 --> 00:06:54,500
done.

94
00:06:55,250 --> 00:06:57,260
Because now we're at 5 5.

95
00:06:57,250 --> 00:06:59,380
It's not less than or equal to four.

96
00:07:00,120 --> 00:07:02,000
So that's all we should need to do there.

97
00:07:02,000 --> 00:07:04,000
There's one small change we could make.

98
00:07:04,050 --> 00:07:06,940
We don't need to say result equals result times.

99
00:07:06,950 --> 00:07:14,340
I we can shorten this by using star equals which is just a shortcut for the exact same thing that we

100
00:07:14,340 --> 00:07:15,240
just had.

101
00:07:15,690 --> 00:07:19,770
And then the very last thing we need to do is return results.

102
00:07:20,280 --> 00:07:27,130
So save let's open it up in the browser refresh and we should have access to our factorial function

103
00:07:27,170 --> 00:07:27,620
.

104
00:07:27,960 --> 00:07:31,480
Let's try factorial of three and we get six.

105
00:07:31,530 --> 00:07:41,340
How about factorial of four or 10 which is 360 2000 or three million six hundred twenty eight thousand

106
00:07:41,390 --> 00:07:46,940
eight hundred which is what we got over here as the solution to factorial of 10.

107
00:07:47,370 --> 00:07:48,860
And then let's try it out.

108
00:07:49,110 --> 00:07:52,060
Factorial of zero.

109
00:07:53,250 --> 00:07:54,630
And we get one.

110
00:07:54,660 --> 00:07:56,230
So it works perfectly.

111
00:07:56,670 --> 00:07:58,510
So that's all we need to do factorial.

112
00:07:58,710 --> 00:08:05,090
As I mentioned you could do this in the other direction where we start with result equal to number.

113
00:08:05,160 --> 00:08:06,420
And we work backwards.

114
00:08:06,540 --> 00:08:08,450
And that would be a very simple modification.

115
00:08:08,670 --> 00:08:17,930
Basically we would change this to be numb and then we would start here at one below numb while I.

116
00:08:17,930 --> 00:08:24,220
Is greater than or equal to 1 i.

117
00:08:24,260 --> 00:08:25,780
Minus minus.

118
00:08:26,190 --> 00:08:31,980
So we're starting result is four and then we're going to multiply by 1 less than the result which is

119
00:08:31,980 --> 00:08:34,140
three and then subtract 1.

120
00:08:34,160 --> 00:08:38,110
So multiply by 2 and then multiply by 1 and then we're done.

121
00:08:38,550 --> 00:08:40,430
So that one definitely works as well.

122
00:08:40,440 --> 00:08:43,870
The only issue that we could run into if we refresh.

123
00:08:43,950 --> 00:08:45,800
Let's try this factorial of three.

124
00:08:45,840 --> 00:08:51,640
It gives us 6 5 gives us 120 and 0 gives us zero.

125
00:08:51,870 --> 00:08:56,030
So there is a problem there because when we started this way.

126
00:08:56,040 --> 00:08:57,760
Result is equal to numb.

127
00:08:57,920 --> 00:09:01,740
If we pass in zero we're starting result as zero.

128
00:09:02,120 --> 00:09:03,250
And then we're done.

129
00:09:03,300 --> 00:09:05,190
We're not multiplying 0 by anything.

130
00:09:05,180 --> 00:09:07,630
And even if we did it would still be zero.

131
00:09:07,910 --> 00:09:12,900
So the solution to that is we could have it simple if statement in here that could be something like

132
00:09:12,960 --> 00:09:18,790
if numb scores equals zero return 1.

133
00:09:18,990 --> 00:09:22,760
And remember if we have a return statement that short circuits everything.

134
00:09:23,000 --> 00:09:24,840
So now it should be good to go.

135
00:09:25,470 --> 00:09:30,540
However I prefer the first solution because it's shorter and we don't have to have that special case

136
00:09:30,780 --> 00:09:36,110
or that we need to do is set result equal to 1 at the beginning and that takes care of everything for

137
00:09:36,120 --> 00:09:36,820
us.

138
00:09:38,860 --> 00:09:42,830
All right so let's tackle the last one which is Khabab to snake.

139
00:09:43,330 --> 00:09:49,080
So remember this is supposed to take a single string and replace all the dashes with underscores.

140
00:09:49,270 --> 00:09:52,720
So it takes Khabab case turns it into Snake case.

141
00:09:52,720 --> 00:09:57,490
So I'm going to go define that function first and I'll just leave it empty to start.

142
00:09:57,550 --> 00:10:01,000
So function Let's make some space here.

143
00:10:01,000 --> 00:10:05,720
Function Khabab to snake takes in a single string.

144
00:10:05,740 --> 00:10:10,350
We'll just call it as TR and then we'll put our logic in there.

145
00:10:10,420 --> 00:10:13,530
But first I'm going to fill up some pseudo code again.

146
00:10:13,540 --> 00:10:22,630
What we want to do is replace all dashes with underscores

147
00:10:25,510 --> 00:10:30,000
and then lastly return as TR.

148
00:10:30,760 --> 00:10:34,640
So the crux of this problem is that we don't know how to actually do that.

149
00:10:34,660 --> 00:10:38,000
How do we replace all of one character in a string.

150
00:10:38,530 --> 00:10:40,000
And again that was deliberate.

151
00:10:40,030 --> 00:10:43,920
I wanted you to have to try and look this up to have to find this out on line.

152
00:10:43,930 --> 00:10:46,000
It is a really really important skill.

153
00:10:46,510 --> 00:10:51,290
So the first thing that I would do is figure out how I want to word my search on Google.

154
00:10:51,370 --> 00:10:55,810
So that sounds like something that would be pretty obvious but it really is going to have a big impact

155
00:10:55,810 --> 00:11:00,790
on the types of results that we get in the rule that I live by is that you want to make your search

156
00:11:00,970 --> 00:11:07,430
as general as possible so we don't want to include things about dashes and underscores or snake case

157
00:11:07,450 --> 00:11:08,870
and Khabab case.

158
00:11:08,950 --> 00:11:14,290
So instead we might want to make a search trying to find out how we replace any character in a string

159
00:11:14,740 --> 00:11:17,620
whether it's a dash or an underscore or a dollar sign.

160
00:11:18,040 --> 00:11:19,660
So let's go ahead and try that.

161
00:11:19,690 --> 00:11:26,240
I'm going to search for javascript replace character string.

162
00:11:26,940 --> 00:11:29,120
And let's see what we get.

163
00:11:29,350 --> 00:11:31,010
Get a few results here.

164
00:11:31,090 --> 00:11:35,020
I'd like to avoid W3 schools so let's try these two.

165
00:11:35,140 --> 00:11:37,740
How do I replace a character at a particular index.

166
00:11:37,750 --> 00:11:40,560
So that's not quite what we're looking for.

167
00:11:40,600 --> 00:11:42,010
This one looks good though.

168
00:11:42,010 --> 00:11:44,610
Replace all commas in a string.

169
00:11:45,280 --> 00:11:51,310
So because we didn't include the dash or the underscore the camel or kabab we got this result which

170
00:11:51,310 --> 00:11:52,840
will work just fine.

171
00:11:52,870 --> 00:11:54,820
This one is to replace all commas.

172
00:11:54,880 --> 00:11:59,890
We'll just need to change one tiny character in the solution rather than replacing commas.

173
00:11:59,890 --> 00:12:02,230
We want to replace dashes.

174
00:12:02,230 --> 00:12:07,830
So the way stack overflow works if you've never seen an up top is the askers question.

175
00:12:07,840 --> 00:12:10,230
So this is not where we want to read our solution.

176
00:12:10,450 --> 00:12:15,220
This is the asking of a question and answers are in reply.

177
00:12:15,220 --> 00:12:19,840
So under this answer section and here we go here's an answer.

178
00:12:19,840 --> 00:12:25,600
This person says you can use a regular expression with the G flag and the great thing is you don't have

179
00:12:25,600 --> 00:12:27,250
to know what it is.

180
00:12:27,250 --> 00:12:29,540
We will talk more about regular expressions.

181
00:12:29,590 --> 00:12:32,250
So let's go ahead and copy this over.

182
00:12:33,340 --> 00:12:38,080
So the first thing you want to do is adapt this code to fit our particular situation.

183
00:12:38,380 --> 00:12:44,260
So we don't need this first variable declaration because we actually have our string coming in as as

184
00:12:44,260 --> 00:12:45,130
TR.

185
00:12:45,460 --> 00:12:52,540
But I will need to do is do the replace on as TR in the first place and then we'll want to change what

186
00:12:52,540 --> 00:12:55,590
we're replacing and what we are replacing it with.

187
00:12:55,600 --> 00:13:01,330
So we want to replace dashes and all that I'll say about this syntax here is that this is called a regular

188
00:13:01,330 --> 00:13:02,100
expression.

189
00:13:02,230 --> 00:13:07,810
And whatever we put inside of the slashes is the pattern that it will look to replace.

190
00:13:07,810 --> 00:13:13,280
So if I put something like double dashes it would look for two dashes in a row.

191
00:13:13,480 --> 00:13:14,470
So that's it.

192
00:13:14,500 --> 00:13:20,530
Regular expressions are much more complicated where I can define very long and advanced patterns.

193
00:13:20,710 --> 00:13:23,230
And then I can replace them with something over here.

194
00:13:23,230 --> 00:13:29,390
So then we need to update what we're replacing the dash with which is just an underscore.

195
00:13:30,070 --> 00:13:38,500
So I'm going to save this to a variable called new string a new as TR is the result of running the string

196
00:13:38,500 --> 00:13:40,350
not replace.

197
00:13:40,450 --> 00:13:46,900
The reason I did that is that dot replaced doesn't actually replace anything in the TR in the initial

198
00:13:46,900 --> 00:13:47,790
string itself.

199
00:13:47,920 --> 00:13:51,520
It just returns a new copy with the replaced data.

200
00:13:51,520 --> 00:13:57,850
So I want to capture that new copy that's returned in the new string variable and then just return new

201
00:13:58,150 --> 00:13:59,520
as TR.

202
00:14:00,190 --> 00:14:02,230
So let's try this out in the browser.

203
00:14:02,230 --> 00:14:11,710
I'm going to refresh my problems that page and try Khabab to snake of Hello Desch worlds and we get

204
00:14:11,740 --> 00:14:13,430
hello underscore world.

205
00:14:13,630 --> 00:14:18,620
Let's try it again with hello world again.

206
00:14:20,420 --> 00:14:23,140
And you can see it replaces all dashes.

207
00:14:23,200 --> 00:14:24,170
So that's it there.

208
00:14:24,310 --> 00:14:29,430
So again the focus in this exercise was really on googling and trying to find information
