Javascript Coding Challenge for Beginners Q8


English (Auto-generated)

Hello everyone welcome back to my javascript coding challenge for beginners. For the last two sessions we were learning new javascript built in functions to solve our problems and as you can see they're much more convenient to use and much simpler. It helps you keep your code clear and concise. So for today we're gonna keep learning these new javascript built in function methods and the topic of choice. Today is an array javascript built in function called for each And what for each does is it basically loops through each iteration in your array. So it's essentially like a four loop but you will see that it takes up less space than a for loop so you can pass the video and come up with their own solution and when you're ready we can come up with a solution together. So for this example we want to return the total sum of theory as you guys have. No, if you guys would use the for loop we can add up to some as well. But for this example we're going to use for each method and now go through the parameters of each one with you. So you can see what the solution is like because we want to get the total sum I guess. Let's set our variable to some square holder and we wanna use the for each method it's up to you guys if you want to use the original function but for me I like to use the arrow function just because it's shorter and our for each method takes in three parameters, one of them is required and the other two is optional. The first parameter is element so you don't have to call this element, you can call this number or item whichever you want. But for this tutorial stick we're just going to call it element. And what element is is basically each iteration of the earring. So the first element would be 15. The second parameter is optional and it's the index of the array. So if you happen to meet the index inside the array, you can use this index optional parameter. The third one is called array. What array is? It's basically the whole array. So between the bracket to the end of this bracket it will return this whole array in here for I guess demonstration purpose. We can do a quick one with array to show you what it's like. So all you have to do is return some plus our and then if we recurrent some here we will see that I'm gonna open the terminal node 08 dot Js So hope we have a problem here which is undefined. We are returning an undefined value. That's weird. Oh because I forgot to save it. Ah So as you can see if we add the sum to the whole rig which is 15 2035 40. We'll see that we're going to have the first iteration which is um we're going to see the first iteration adding to 0 to 15, 25 30 and 40. And then the iterations will go on for five times, so that's why we have five of them. However, what we want is the total sum. We don't want zero adding onto the array. So let's take these two parameters off and we're just gonna simply add up the elements like we do in a for loop. So we're gonna save this so we don't get in defining and I'm gonna clear how we're gonna run no jf. So as you can see we add them when we do get to 10 and this is how the four each method works. It's good to know this method because in the next challenge we're gonna be using the for each method with another Javascript books and function to get her answer. So thank you guys for watching this challenge. If there's any questions you can leave a comment below and I'll try to answer it. So when you're ready, click next and we'll go to the next challenge
10 Views 0 Likes 0 Comments

Use Javascript builtin functions to solve the problem

Leave a comment (supports markdown format)