All right.
So let's go over the solutions to these three problems.
Let's start with the first one is even so to get started here I'm going to go ahead and open up supply
.
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
that.
Yes.
So next up I need to make this solution that J file and I'm going to save that solution.
Yes in the same place and quite safe.
So again we're starting with is even.
So I'm going to declare the function is even
and it needs to take a single argument.
Let's just call it numb and then we want to return true
if even return false otherwise.
So there's a few ways of doing this.
The first one is to do it as explicitly as possible.
So we're going to say if numb mod 2 is zero remember that's how we tell if something is even if the
remainder of dividing two into that number is zero if there is no remainder.
That means that it's even.
So if that's the case we can return TRUE else return false.
Just like that.
And I'm going to move this up here
just to make it clear what corresponds to what.
Just like that.
So we can do this.
And I'm actually not going to call my function just yet.
I'm going to open it up in the browser to show you something slightly new.
So if I open up my console I actually have access to is even in my console here.
So I type is even with no parentheses and it tells me yes I know about is even Here's the code and if
I add the parentheses I need to pass that number in like 5 and I get false.
And I can do it again with four or with one or with ninety nine and it works great.
So there's two things there are is even works.
So that's awesome.
But more importantly I want to show you that we can define functions and code in our files.
And then when we open those files up in the browser.
In this case our aged him L5 that's connected to our javascript file.
I can then access those functions.
So there is a small refactor that we can make here.
I'm going to copy this code commented all out and I'm going to shorten this up so I'm just going to
show you the solution first and then we'll go over how it works.
So this is substantially shorter return.
Number two equals equals equals zero.
So let's refresh the page.
Make sure that it works is even for 4 is true is even if 3 is false.
Still works the same way.
So the way that it actually works this line here.
Return Nahm made to triple equals zero.
It's going to evaluate this first and that's a true or false statement it's a boolean statement.
So numb to Tripoli equals zero is either going to be true or false.
And then we're just returning true or false.
So rather than checking if it's true return true and if it's false return false we just turn the value
immediately.
So that's a nice little shortcut there.
So next up we have factorial.
So I'm going to go ahead and start by defining my function called factorial function factorial and we
know need to take a number.
So down here I'm going to write a note.
So if I did factorial of four that should give me four times three times two times one and I'm going
to come with that.
So inside of our function I'm going to write a few lines of pseudo code.
The first thing that we know we want to do is define a result variable and then the very last thing
in the function is return the result variable.
So in-between that's important and that's where we're going to calculate
factorial and store value in result.
So we have a few approaches in how we can calculate factorial.
We could either start by setting result to be one at the very beginning and then multiplying it by two
and then multiplying it by three and then by 4 or we could go the other way around like we have here
.
And I'm going to start by doing it this way so you need to a result variable var result equals 1.
And then what we need to do is multiply that result by every number between 1 and that number.
So one in four or one in ten whatever is passed in as No.
So to do that we'll want to use a loop we could use either a for loop or a while loop.
I'm going to use a for loop here because it's a little bit shorter.
So for far and let's just call it I.
And we could start I as one.
So I'll do that first.
And we're going to keep multiplying while I is less than or equal to numb then we're going to add 1
to i every time.
And then all that we want to do is multiply result by I.
So if we step through this if we did it for four factorial of four result starts as one right here then
we're creating I start says 1 and we multiply result by.
So that's just 1 times 1 which I noticed is unnecessary.
So we might want to just start this at 2.
So the first time through we multiply by times result.
So we get 1 times 2.
Next time through I is now three because we added one.
So that is still less than the number which is four.
So we multiply again result equals the current value a result which is this times 3.
So then we have 6 and then the next time through I now is four.
So four is less than or equal to four.
That is true.
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
done.
Because now we're at 5 5.
It's not less than or equal to four.
So that's all we should need to do there.
There's one small change we could make.
We don't need to say result equals result times.
I we can shorten this by using star equals which is just a shortcut for the exact same thing that we
just had.
And then the very last thing we need to do is return results.
So save let's open it up in the browser refresh and we should have access to our factorial function
.
Let's try factorial of three and we get six.
How about factorial of four or 10 which is 360 2000 or three million six hundred twenty eight thousand
eight hundred which is what we got over here as the solution to factorial of 10.
And then let's try it out.
Factorial of zero.
And we get one.
So it works perfectly.
So that's all we need to do factorial.
As I mentioned you could do this in the other direction where we start with result equal to number.
And we work backwards.
And that would be a very simple modification.
Basically we would change this to be numb and then we would start here at one below numb while I.
Is greater than or equal to 1 i.
Minus minus.
So we're starting result is four and then we're going to multiply by 1 less than the result which is
three and then subtract 1.
So multiply by 2 and then multiply by 1 and then we're done.
So that one definitely works as well.
The only issue that we could run into if we refresh.
Let's try this factorial of three.
It gives us 6 5 gives us 120 and 0 gives us zero.
So there is a problem there because when we started this way.
Result is equal to numb.
If we pass in zero we're starting result as zero.
And then we're done.
We're not multiplying 0 by anything.
And even if we did it would still be zero.
So the solution to that is we could have it simple if statement in here that could be something like
if numb scores equals zero return 1.
And remember if we have a return statement that short circuits everything.
So now it should be good to go.
However I prefer the first solution because it's shorter and we don't have to have that special case
or that we need to do is set result equal to 1 at the beginning and that takes care of everything for
us.
All right so let's tackle the last one which is Khabab to snake.
So remember this is supposed to take a single string and replace all the dashes with underscores.
So it takes Khabab case turns it into Snake case.
So I'm going to go define that function first and I'll just leave it empty to start.
So function Let's make some space here.
Function Khabab to snake takes in a single string.
We'll just call it as TR and then we'll put our logic in there.
But first I'm going to fill up some pseudo code again.
What we want to do is replace all dashes with underscores
and then lastly return as TR.
So the crux of this problem is that we don't know how to actually do that.
How do we replace all of one character in a string.
And again that was deliberate.
I wanted you to have to try and look this up to have to find this out on line.
It is a really really important skill.
So the first thing that I would do is figure out how I want to word my search on Google.
So that sounds like something that would be pretty obvious but it really is going to have a big impact
on the types of results that we get in the rule that I live by is that you want to make your search
as general as possible so we don't want to include things about dashes and underscores or snake case
and Khabab case.
So instead we might want to make a search trying to find out how we replace any character in a string
whether it's a dash or an underscore or a dollar sign.
So let's go ahead and try that.
I'm going to search for javascript replace character string.
And let's see what we get.
Get a few results here.
I'd like to avoid W3 schools so let's try these two.
How do I replace a character at a particular index.
So that's not quite what we're looking for.
This one looks good though.
Replace all commas in a string.
So because we didn't include the dash or the underscore the camel or kabab we got this result which
will work just fine.
This one is to replace all commas.
We'll just need to change one tiny character in the solution rather than replacing commas.
We want to replace dashes.
So the way stack overflow works if you've never seen an up top is the askers question.
So this is not where we want to read our solution.
This is the asking of a question and answers are in reply.
So under this answer section and here we go here's an answer.
This person says you can use a regular expression with the G flag and the great thing is you don't have
to know what it is.
We will talk more about regular expressions.
So let's go ahead and copy this over.
So the first thing you want to do is adapt this code to fit our particular situation.
So we don't need this first variable declaration because we actually have our string coming in as as
TR.
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
we're replacing and what we are replacing it with.
So we want to replace dashes and all that I'll say about this syntax here is that this is called a regular
expression.
And whatever we put inside of the slashes is the pattern that it will look to replace.
So if I put something like double dashes it would look for two dashes in a row.
So that's it.
Regular expressions are much more complicated where I can define very long and advanced patterns.
And then I can replace them with something over here.
So then we need to update what we're replacing the dash with which is just an underscore.
So I'm going to save this to a variable called new string a new as TR is the result of running the string
not replace.
The reason I did that is that dot replaced doesn't actually replace anything in the TR in the initial
string itself.
It just returns a new copy with the replaced data.
So I want to capture that new copy that's returned in the new string variable and then just return new
as TR.
So let's try this out in the browser.
I'm going to refresh my problems that page and try Khabab to snake of Hello Desch worlds and we get
hello underscore world.
Let's try it again with hello world again.
And you can see it replaces all dashes.
So that's it there.
So again the focus in this exercise was really on googling and trying to find information