Javascript Coding Challenge For Beginners Q11

Transcript

English (Auto-generated)

Hey everyone welcome back to man, java script coding challenge for beginners. And this is question 11. We're going to keep working on the java script built a malfunction from before. So in the earlier example you guys saw that we were able to concatenate the first and last thing within our map function and return. And we got the results of the first and last name of our pet for this one. We'll go a little bit more in depth and add a bit of math operations into it. So for today's challenge, we will use map to find the square root of each element inside this area. They also want to add five more to it. So pause the video and come up with their own solution. And when you're ready we can work on the solution together. So now that you're ready, let's tackle the solution. So there are a few things I want you guys to remember about map and in this example we will try to demonstrate it as much as possible. So let result people are that math and do you want to go through each iteration? So in javascript we have something called math dot square root that allows us to square root any number that we put inside. So we want to do is want to place the element inside our current iteration and have a square root for us. But we also want to add five. Mhm And now we want to return results to get our results. So let's run this in the terminal. So as you can see using map, we can use math operations and we can also use other functions such as map square root together. Answer? It works for both strings and number and objects as well. But more importantly, you guys have to remember that Map actually makes a new array, so this does not um mutate the original array, this creates a new array. And another thing I want you to know about Map is what if we only wanna Return a number that's greater than 99. So let's see what happens if we do that. So if element is greater than 99 we want to perform this function. So let's save the code, we'll clear it and we'll run it again. So one thing about map is regardless of what you want to return, Map will return the exact same amount of elements. So if you can put five elements like I did in this array, it will give you back five elements. Even if this function does not run. So what happened is you will get undefined elements unless it meets the requirement. So in our case only one number is greater than 99. So we only got an answered 15 for one of So the square root of 100 is 10. 10 plus five is 15. So this is correct. However, since the other functions, I mean the other elements to then pass the elements greater than 99 condition, we will have undefined for them because map always returns the same amount of elements as given. So that is one thing good to remember. But you guys must be wondering what if I only want The result number 15 in my rig? Well, that is up for the next video. In the next video, we will work on a javascript built in function called filters and filter will exactly perform what we want. So thank you all for watching. When you're ready, you can go onto the next challenge.
10 Views 0 Likes 0 Comments

Use map() function to solve the coding challenge

Comment
Leave a comment (supports markdown format)