‫All right guys.
‫So we're just going to do a couple more challenges.
‫These are very algorithm like I just thought it would be nice to do this to wrap the section up.
‫So there's three challenges.
‫I'll go over them real quick.
‫And then you can pause the video and try them out.
‫So the first one is to write a function called Fahrenheit to Celsius that takes in a Fahrenheit temperature
‫as an argument and returns the temperature in Celsius.
‫And I put an easy version and a version that's slightly hotter.
‫So for easy you can create just a regular named function called Fahrenheit to Celsius that takes in
‫Fahrenheit, returns the the converted temperature in Celsius, and then print to the screen something
‫like 68 Fahrenheit equals 20 Celsius.
‫And you can use the the entity, the degrees entity, although it doesn't show it here because it's
‫a markdown file but it's ampersand deg semicolon.
‫All right.
‫And then for extra points you can convert that to an anonymous and or an arrow function.
‫Now for the hotter version, just slightly harder.
‫You can create a variable in the global scope that holds the base temp of 32, and then create an anonymous
‫function slash closure that takes in attempt to convert just like the other version, but use that base
‫variable from the outer scope to calculate it to Celsius.
‫And remember, we can do that using the use keyword and then print it to the screen.
‫Now the hint is very important because you might not know this.
‫This is the formula for converting Fahrenheit to Celsius right here.
‫So it would be f minus two and parentheses multiplied by five over nine.
‫So basically you just have to have that in your in your function and you'll get the answer.
‫But I wanted you to just format it in different ways.
‫Named function arrow function closure.
‫ET cetera.
‫So just experiment with it.
‫Second one is pretty simple.
‫We're just going to print an array of names in uppercase.
‫So you'll have a function that will have some kind of loop.
‫It's up to you if you want to use for each or for or while, and just output the names onto the screen
‫and make them all uppercase, and you can use string to upper for that.
‫So the last one is a little more complicated.
‫It's called find the longest word.
‫So you'll have a function that will take in a sentence as an argument.
‫And you want to return whatever that longest word is.
‫So this sentence right here, if we pass it into our function, we should get jumped because that's
‫the longest word as far as hints you can use explode.
‫I probably shouldn't have said you will need to because there's multiple ways to do this, but what
‫I'm going to do is use the explode function to split the sentence into an array of words.
‫Okay, you can use the string Len function to find the length of each word, and then you can loop through.
‫Or I should say while you're looping through the array of words, you'll have a conditional check to
‫see if the current word that you're looking at in the current iteration is longer than the longest word
‫that you have found so far.
‫So you want a variable to hold whatever the longest word is, and basically just run that condition
‫in each iteration.
‫All right.
‫So pause the video, try it out on your own and then come back.
‫All right.
‫So I just have all the in comments here.
‫The same thing I just read you.
‫So let's start with the Fahrenheit to Celsius.
‫And I'm going to start with just a regular named function.
‫So we'll say Fahrenheit to Celsius.
‫And then let's pass in.
‫I'll just call this F.
‫So that's the Fahrenheit or the whatever we want to convert.
‫And then let's create a variable called C.
‫And all we need to do is grab that formula.
‫So this right here.
‫Pop that in there and then just replace this with our Fahrenheit temperature.
‫Okay.
‫And that will give us the Celsius value.
‫So then we can just return whatever C is and then to print it out.
‫So I'm going to just echo and I'm going to actually have a variable that holds what we want to convert
‫what we want to pass in.
‫So let's say degree degree to convert.
‫And we'll set that to 68.
‫Okay.
‫So now down here let's echo out.
‫So basically I want it to say 68 Fahrenheit equals whatever 20 Celsius.
‫So let's say degree to convert.
‫And then I'm just going to use string concatenation here.
‫And I'm going to use the entity deg like that.
‫So that'll give me the little degree symbol.
‫And then I want an F because it's Fahrenheit.
‫And then we'll just do equals.
‫And then we'll concatenate our function.
‫So Fahrenheit to Celsius.
‫And we'll pass in that degree to convert.
‫And then we want to concatenate onto that the degree symbol and then see.
‫Okay.
‫And that should do it.
‫So let's save that.
‫Let's reload and we get 68 Fahrenheit equals 20°C.
‫Now to convert this to an anonymous function.
‫Let's do that first.
‫So we're going to get rid of the function keyword and let's say Fahrenheit to Celsius.
‫So it's a variable now and it's going to equal a function.
‫And then it's going to pass.
‫We're going to pass that in and that should do it.
‫Now we just need to.
‫Put a semicolon here and then just update the function call to use the variable.
‫Reload and we get the same thing.
‫Now to make this an arrow function, we can replace function with just fn.
‫We can add an arrow here and then get rid of the ending curly brace.
‫And then instead of putting this into a variable, we'll just return the formula.
‫So we'll get rid of that.
‫And then get rid of the c equals get rid of that opening parentheses.
‫And there we go.
‫So if you actually needed this helper function this is the way I would do it.
‫Because it's just so it's one line very simple.
‫Now let's do the version that's a little bit more difficult using a closure.
‫So I'm going to comment that out because I still want to use this part.
‫And then I'm going to create a variable called base temp and set that to 32.
‫And that's going to represent this 32in the formula.
‫So let's create our function that holds our variable which will be Fahrenheit to Celsius.
‫And we want to set that equal to an anonymous function.
‫And that's going to take in whatever that Fahrenheit temp is.
‫Now to use that base temp that's in the outer scope, I'm going to add use.
‫And then pass in here base temp.
‫All right.
‫And then we can simply return.
‫And we want that Fahrenheit variable minus whatever base temp is, which is 32.
‫And then we want to multiply by five over nine.
‫Okay.
‫And then we just need a semicolon at the end here.
‫And then that should give us the same result.
‫All right.
‫So that's the first one.
‫The next one is really simple.
‫We're just going to have we're going to pass in an array of names.
‫And we want to print them onto the screen and have them all be uppercase.
‫So let's say function print.
‫Print names to uppercase, it's going to take in an array of names.
‫We're going to loop through that with a for each and say names as.
‫Name.
‫And then what I'm going to do is create a variable called uppercase names or uppercase name.
‫Set that to str to upper, and then pass in the current name and the current iteration.
‫Then I just want to echo out whatever uppercase name is.
‫And I'm going to concatenate a line break on to that okay.
‫Then we can go outside the function create our names array.
‫So we'll say Alice.
‫Uh, Bob.
‫Steve, and I don't know Margaret.
‫All right, so now we can just call print names to uppercase pass in names.
‫Reload and we should see them printed on the screen in all uppercase.
‫So really really simple simple challenge.
‫So the next one is a little bit harder.
‫We're going to find the longest word.
‫So pass in a sentence and we want to output whatever the longest word is.
‫So let's create a function called find longest word.
‫It's going to take in a sentence.
‫And the first thing I'm going to do is split that sentence up into an array an array of words.
‫So we'll call it words.
‫And we're going to use explode to do that.
‫And explode is going to take in whatever we want to split the the string as into an array.
‫And that's going to be a space obviously, because we want each word and then we want to pass in sentence,
‫which is our string.
‫Okay.
‫Now we have this array.
‫I'm going to just initialize a variable called longest word and set it to just an empty string to begin
‫with.
‫So when we loop through the the array of words, if it's longer than the current value, then we're
‫going to replace that with with that value.
‫So let's say for each.
‫And we want to say for each words as word.
‫And I'm just going to trim it.
‫So words.
‫I'm sorry word.
‫We want to set that to trim.
‫So this will just trim any white space.
‫So trim word.
‫Then I'm going to have my if statement here.
‫And we want to check the length so we can do str Len we want to check the length of word.
‫Right.
‫And we're going to say if that is greater than the length.
‫So string Len of the the current longest word, whatever's in that variable or if longest word is equal
‫to an empty string because that's what we're starting it as.
‫So basically what will happen actually let me just finish this up.
‫So longest word we're going to set that to word.
‫So what will happen is it starts out as an empty string.
‫And this is going to match right.
‫Because this is true.
‫So it'll put whatever the first word is will go into that longest word variable.
‫Then it's going to iterate again and it's going to compare whatever's in that longest word to the current
‫word, okay.
‫And if it's greater, if the current word is greater, then whatever is in this variable will be replaced
‫with that.
‫All right, so now we just need to go outside of the for each and just return whatever that longest
‫word is.
‫All right.
‫So now to check this out.
‫Let's just say we'll do echo and then find longest word.
‫And let's pass in.
‫I'll pass in the example.
‫Right here.
‫So this should give us jumped if we did this correctly.
‫So let's pass that in.
‫Reload and we get jumped.
‫All right if I say the quickest.
‫And reload.
‫Then we get quickest because that's now the longest word.
‫All right, so that's it.
‫Let's move on to the next section.