در حال بارگذاری ...

Minutes to Seconds Function0:27

So I'll make sure to give you some resources at the end of this video. So each exercise will just be a simple function that solves some sort of problem. So let's start off really simple. Let's define a function that converts minutes into seconds. So convert minutes into seconds. And this takes in one param, takes in the minutes, which should be a number. So if you want, you can pause the video and go ahead and try this on your own. So to convert minutes into seconds, you simply multiply by 60. So how about we say const seconds equals minutes times 60, and we can return that. So return seconds.

So how about we say const seconds equals minutes times 60, and we can return that. So return seconds. And to test out a few values, we can console.log convert minutes into seconds. And how about 2 minutes should be 120 seconds, and 3 minutes should be 180. So let's see if that works. And that is correct. And we can even shorten our code here and just return minutes times 60 without the variable. We can get rid of this, and that should still work. Okay, let's move on to our next problem here. I'm going to comment these out just to keep our console clean.

Appending Question Marks1:33

Okay, let's move on to our next problem here. I'm going to comment these out just to keep our console clean. And let's make one called ensureQuestion. And this takes in a string, let's name it str. And given this string, if the last character does not have a question mark, then go ahead and add one. But if it already does, then just leave it. So we're going to need access to the last character of the string being passed in. And we can do that in a few ways. So let's make a new variable called lastCharacter or lastChar is fine.

And we can do that in a few ways. So let's make a new variable called lastCharacter or lastChar is fine. And we can use the charAt method. So str.charAt, and the lastCharacter is going to be str.length minus one. So remember, strings and arrays are zero based. So the first character starts at zero, and the last character is the length of the string minus one. So that's why we have length minus one for the lastCharacter. And now that we have this, we can do a conditional here or an if statement. So say if the condition is if the lastCharacter is equal to a question mark, so a string question.

And now that we have this, we can do a conditional here or an if statement. So say if the condition is if the last character is equal to a question mark, so a string question mark, then go ahead and return that. So we don't have to do anything to it. So return s or str. But if it's not a question mark, then go ahead and add that to our string. So let's return. We can use template strings here. So we want to return the string. So str plus a question mark at the end.

So we want to return the string. So str plus a question mark at the end. Okay. And we can test this out with a few console.logs. Let's console.log ensureQuestion with the string yes. So that should add a question mark to the end. Same with this one for no. But if there's already a question mark, then it should just keep that string. So let's save that. And the output is correct.

So let's save that. And the output is correct. Now we can even shorten our code if we like. When we return something, then the code stops executing. So we don't even need this else statement here. So we have this one condition here. If it reaches here, then it just stops. But if it doesn't, we can just remove the else statement and return the string plus a question mark. And this should still work.

a question mark. And this should still work. Okay, save that still works. Or if you want, you can even use a ternary to make this one line. And when solving problems like this, there's always more than one solution. So be sure to keep that in mind. So let me comment this out. I'll show you the ternary way. So the condition is lastChar is equal to a question mark. And if that's the case, then we want to return the actual string.

So the condition is lastChar is equal to a ?. And if that's the case, then we want to return the actual string. Sorry, that should be a ?. And then the else case is the string plus the ?. So pretty much just this. So let's grab this, put this here, and we want to return this. So we can say return. And that's basically the same thing by using a ternary. And I'll make sure to throw this up on GitHub if you want to reference the code here. Move on to our next problem here.

Finding Max in Array4:19

And I'll make sure to throw this up on GitHub if you want to reference the code here. Move on to our next problem here. I'm going to comment this out again just to keep our console clean. And this one will return the max number given an array of numbers. So I'll say function max of numbers, and numbers will be an array of numbers. So we'll call it like this console.log, max of numbers, and the parameter should be an array of numbers, say [1, 2, 3], so the max should be 3 in this case, and so on. So let's say [10, 100], obviously 100 should be returned here. And let's assume that there's always one number in the array, or at least one number in the array.

And let's assume that there's always one number in the array, or at least one number in the array. So all we have to do is go through each item in the array, say, for example, this array, and keep track of which one is the maximum value. So how about we define a variable for that? So say let maxNumber, and how about to start, let's make the maxNumber the first number in the array. So we can say numbers[0] to get the first number. And then we can go through each one and compare the current maxNumber to the current iteration of the loop.

And then after that, there's nothing left in the array. So three is the greatest number in the array. And if you were to flip this, it will still work, say, for example, three and two here. So this one would be true. But once we get here, two is not greater than three. So our max number should still be three. So let's see how we can do that with our code. So we have to use a for loop, or some sort of loop that iterates through the entire array. So let's stick with the for loop for now. And let's say let i equals 0, or we can actually start at 1 here, but let's just start at 0.

So let's stick with the for loop for now. And let's say let i equals 0, or we can actually start at 1 here, but let's just start at 0. And we want to go through the entire array. So let's say i is less than numbers.length. And we can increment i, so i++. Now we need a conditional here to check if the number at the current iteration is greater than the current maxNumber. So I hope that makes sense. So the current number is going to be numbers[i]. If this is greater than the current maxNumber, which we stored in this variable, then go ahead and update it. So we can say maxNumber is now numbers[i].

If this is greater than the current max number, which we stored in this variable, then go ahead and update it. So we can say maxNumber is now numbers[i]. Okay. And after the loop is done, so here we can return that maxNumber. So let's say return maxNumber. And if I did this correctly, this should be 3 and this should be 100. And it is. Cool. So let's go through this one more time. With this example here, I'll put one more number in here, let's say 0. So maxNumber is going to be the first value in the array, which is 1.

With this example here, I'll put one more number in here, let's say zero. So maxNumber is going to be the first value in the array, which is one. And now we're going to go through each value in the array using a loop. So first, we're going to check if one is greater than maxNumber. That's not the case. So go ahead and go to the next iteration. Is three greater than maxNumber? It is. So we update the maxValue to three. Next iteration is two greater than three.

So we update the max value to three. Next iteration is two greater than three. That's not the case. So nothing happens. And then for our last iteration is zero greater than three. It's not. So go ahead and return three here. And if you wanted to, you can even replace this for loop with a foreach loop, which is a bit cleaner in my opinion. So let me come this out. Pretty much the same thing, but we're using a foreach loop.

So let me come this out. Pretty much the same thing, but we're using a foreach loop. So we say numbers not foreach. So just go through the or just go through each item in the array. And instead of working with indexes, we can just work with the value itself. So we can say number. So for each iteration in the loop, we have access to the actual number, which is named number in this case. And this is a callback. And we can do pretty much the same thing we did in our for loop. So if number, so the current number in the iteration or in the loop is greater than the maxNumber, then go ahead and update the maxNumber.

All right, let's move on here. What's next? Actually, let me show you one more solution for this. I'll put this back. And we'll comment this out as well. Or we'll comment everything out, actually. But we'll just use the Math.max method. So let's say return Math.max. But Math.max doesn't take in an array. It takes in multiple values like this.

Alternating Character Case9:41

So to do that, we can use three dots and say numbers here. And this should work as well. And it does. So again, multiple solutions for the same problem. OK, let's move on here. Let's comment this out. And we'll make a new function called spongeCase. And this takes in a string as well. Or how about we name it sarcasticCase? SarcasticCase.

Or how about we name it sarcasticCase? SarcasticCase. So all I want to do is take this string and alternate between uppercase and lowercase for each character. So if I had a string that said hello there, I want to convert this to say H-E-L-L-O-T-H-E-R-E with alternating case. So you'll see a lot of memes out there where this is often interpreted as sarcastic or being sarcastic. So let's go ahead and do that. So I'm going to make a variable here called newString. And we'll build up this string as we go along. But for now, it'll be an empty string.

And we'll build up this string as we go along. But for now, it'll be an empty string. And we want to go through each character in the string. So again, we can use a for loop for that. So let's say for let i = 0, i

1 would be odd, which would be this character. We'll change that to lowercase, and so on and so forth. So to detect if it's even, we can use the modulus operator. So if i% is modulus, 2, so if i % 2 equals 0, then that's the even case. So let me just put that in comments, even number. And we can do what we want here. So in this case, we want to convert the character at 0, or at this index, to uppercase.

So in this case, we want to convert the character at 0, or at this index, to uppercase. So let's go ahead and do that. So let's build up our string here. So let's say newString equals newString, plus, again we're concatenating, stri.toUpperCase(). Okay. But if it's odd, so else. Pretty much the same thing, but we change it to lowercase. Okay, to lowercase.

Pretty much the same thing, but we change it to lowercase. Okay, to lowercase. And we can return that newString. So after the for loop, return newString. Okay. Let's try that out. Let's console.log a few examples here. Sarcastic. Case. Let's just say hello.

because we're updating it. Okay. And it does seem to work. Cool. So again, we're using this for loop to go through every character in the string, for example, this string. And then we're checking if the index is even or odd. And if it's even, change it to uppercase. If it's odd, change it to lowercase.

And if it's even, change it to uppercase. If it's odd, change it to lowercase. And we're using a temporary $variable for this and building up the string as we go through the loop. Again, if you wanted to make this shorter, we can make use of a ternary. So let me just comment this out for now. Let's go ahead and update our newString. So newString equals. And what's the condition?

So newString equals. And what's the condition? The condition is imod2. If that's the case, then go ahead and update it to uppercase. So we can grab pretty much this. Space it in here. But if it's not, let's say colon. And it's the same thing, but to lowercase. So let's change this to lowercase.

Removing Spaces from String13:46

And it's the same thing, but to lowercase. So let's change this to lowercase. And that should be the same thing. But now we're making use of ternaries. Okay, let's move on to our next problem. Let's comment this out. And this one is called nullSpace. And this one's pretty straightforward. Just take in a string and remove any spaces that it has.

Just take in a string and remove any spaces that it has. So it's similar to this, where we want to go through each character in the string and then update it if necessary. So in this case, we want to check if it's a space. And if it is, then remove it. So we can pretty much do the same thing here. So let newString equals an empty string. Okay.

So let newString equals an empty string. Okay. We can loop through it again. So let me just grab... Actually, we'll do it from scratch. So for let i equals 0, i is less than str.length, and then i++ to loop through this string and go through every character.

to loop through this string and go through every character. And all we're doing is checking if it's not a space. So if the current character... So if string or str[i], so the current character does not equal, so not equal, so ! equals equals, so if it's not a space, then we can build up the string. So we can say newString equals newString...

then we can build up the string. So we can say newString equals newString... Sorry, not newString, str, i. And that's pretty much it. And after the for loop, we can return the newString. Return newString. Okay. Let's try that out. Let's console.log no space with a string of hello there. And let's do one more for...

with a string of hello there. And let's do one more for... I am cool. So if I did this correctly, this should remove the spaces. And it looks like I have a bug here. I forgot to say newString here. So newString equals newString, and we want to concatenate the current character. Let's try that again.

and we want to concatenate the current character. Let's try that again. And now it works. So again, just to visualize what's happening, we have a string that's empty called newString, and we're going through each character in the string. If it's not a space, then go ahead and add that to the newString temporary variable. So in this case for hello, it's not a space, but when it gets to the actual space here,

So in this case for hello, it's not a space, but when it gets to the actual space here, then it ignores this if statement because it's false. It is a space, so it doesn't get added to the newString variable. And that will result in getting rid of the spaces. Let's take a look at an alternate solution here, which makes use of split and join. And the split method, let's just return that for now.

And the split method, let's just return that for now. return str.split. The argument it takes is what to split the string on. And this will convert it to an array based on the argument here. So for example, for hello there, split will return an array of two items, the first being hello. We said we want to split on space,

the first being hello. We said we want to split on space, and then the second item will be there. So let's see what we get here. It should be an array of hello there. Okay, and the join method joins each item in the array based on the character that you give it. So in this case, we want to join on an empty string,

So in this case, we want to join on an empty string, and that should get rid of the spaces. So if I save that, we get the same thing. Again, just wanted to show you an alternate solution for this problem. Let's do a few more here. Again, let me comment this out. And let's keep working with numbers here.

Square and Sum Array17:19

Again, let me comment this out. And let's keep working with numbers here. And this one will be squareSum. So given an array of numbers, say numbers, square each item in the array, and then sum them up. So if we were to say console.log squareSum an array of numbers,

3, so that should be 9 if we square it. Say 5, should be 25. 25 plus 9 is 24. Okay. Again, I'll show you two ways you can do this. How about we start with a temporary variable here, which keeps track of the sum. Let's name it result.

which keeps track of the sum. Let's name it result. And let's say 0 to start. And again, we want to go through each number in the array, and then sum up the square of each item. So again, we can use a for loop here, but I'll use a foreach loop to start. So say numbers.foreach. So for each number in the array, this takes in a callback.

So for each number in the array, this takes in a callback. Same at number. Okay. Let me just scroll up here. And we want to update the result. So result equals result plus, we want to add the square of the number. So let's put this in brackets. We can either do number to the power of

This is just a simple explanation with no code.

Did I say 24 here? I meant 34, obviously. And you can even shorten this to use the += syntax. Instead of result = result + this, we can say result += this. And that should be the same thing. Save that. Still works. Or we can take a totally

Save that. Still works. Or we can take a totally different approach here and make use of more declarative functions like map, filter, and reduce. So again, let me just comment all of this out. This is totally fine. I'm just showing you an alternate solution. So how about we first square all of the numbers in the array. So let's say return, and we can use

all of the numbers in the array. So let's say return, and we can use map for that. So numbers.map. And we want to square each number in the array. So we'll use an implicit return here. So number * number, or number to the power of 2. So that should return the squares of the numbers

So that should return the squares of the numbers in the array. And it does. Now we can chain on another method here called reduce, which sort of does the same thing as what we're doing here, but making use of a callback. So at each step, it's adding the current array value to the result of the previous step. And this might seem confusing

array value to the result of the previous step. And this might seem confusing if you haven't used reduce before, but again, I'm just showing you an alternate solution here. Let me just put this on its own line. And we can say array.reduce. And this takes in a callback, which takes two params, previousValue and currentValue.

previous value and current value. And we want to update or sum the previousValue with the currentValue. And then we start at 0, which is the second param. And if I did this correctly, we should still get the same result here. So 14 and

And if I did this correctly, we should still get the same result here. So 14 and 34. There should be no semicolon here. Save that again. And since this has two params here, we need brackets around them, so let me add that. Save that one more time. And we do get 14 and 34. I believe I do have a Larabit episode where I

Summing Positive Numbers20:49

get 14 and 34. I believe I do have a Larabit episode where I go through writing more declarative code using map, filter, and reduce. So be sure to check that out if you're curious about these methods here. And let's do one more example here, which is similar to this one here. Let's say function positiveSum.

Let's say positiveSum. And again, this takes in an array of numbers, or an array of numbers, and sums up only the positive ones. So again, that's console.log positiveSum. So if I had an array of 1, 2,

2, negative 4, and 5. So that should be 10. Okay? So again, we'll start off with the for loop approach, and then we'll use reduce again, similar to how we did before. So let's define a variable here named total or sum. Total is fine. And we'll go through the array here. And again, I'll use a foreach here.

And we'll go through the array here. And again, I'll use a forEach here. You can use a for loop if you like, but we can say numbers.forEach and number. Okay? And now we only want to sum it up if it's greater than 0, or if it's positive. So if number is

it's positive. So if number is greater than 0, then go ahead and add it up. So let's say total equals total plus number. Again, we can shorten this and say plus equals number. So total plus equals number. Okay?

total plus equals number. Okay? And after the for loop, we can return total, and that should be correct. So after the foreach, I mean, let's return total. And let's see if we get 6 and 10. And we do. Again, this is totally

6 and 10. And we do. Again, this is totally fine, but if you want to take the declarative approach and make use of map, filter, and reduce, let's go ahead and take a look at that. So we'll comment this out. So how about first we get rid of all of the numbers that are not positive. So we can make use of filter to do that. So we can

are not positive. So we can make use of filter to do that. So we can say return numbers.filter and the condition is so this is a callback. And let's say only keep the numbers that are greater than 0. So number greater than 0. Let's see what we get here. So we should get 1, 2,

This is just a simple explanation with no code.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟