Javascript Coding Challenge for Beginners Q6

Transcript

English (Auto-generated)

Hello everyone welcome back to my javascript coding challenge for beginners today, we will be using javascript built in functions to solve our problems. Um but before that if you don't know what javascript built in functions, be sure to check out on M D M documentations are on W three school. They have very good explanations for what javascript built in functions are. Um for td we will try to reverse a string from happy easter to, I don't know if I pronounced that right, but we're basically just reversing it. So write down your own solution and when you're ready you can impose the video and we will solve the problem together. So now that you're ready, let's solve the problem together. For those who have never used javascript built in functions, we can do it with a for loop. So let's try it with the normal way first. So let's string equal to an empty string because we want to hold the string variable later. And for let I equal string dot length minus one. We want to start from string dot length minus one because we want to start at the end of the string because by reversing it, we want to start at the end and we want to append every single letter so then we can get the reverse portion. So for this one we are starting at the end. So string down length -1 and I is greater or equal to zero. Why is it greater than equal to zero because when you start at the end you're gonna end at the index zero and we also want to do I minus minus because we're starting at the end, we want to go down by each iteration. So if this was the reverse, we were too, we we will not be using it this way. So now that we wrote the for loop, all we have to do is append our string to each letter. So string I so if you think about it, the first letter would be r the second letter we have penned would be the third letter will be T. And in the end we would we should be able to get this answer. So let's return string and check out our answer. So we have a new terminal node 06 and as you can see we have the same answer as the one on top. However, Javascript built in function makes our lives even easier. So let's comment it out and we will write the function of solutions again. Oh hello, Didn't I guess corrected for me. So the string And for javascript built in function, we have a function called split. So what? Split classes? It converts a string into an array. So if you put a space it will convert into a word. If you don't put a space, it will convert it into a single character throughout the array. You have to convert it into an array function because there is a javascript array function that can help you reverse the strength. So you don't even have to loop it. All you have to do is type reverse. But in order to use this reverse built in function, you must convert it into an array verse if you have understand this so far. So lastly you will know that because you're it's an array now and it's reversed. All we have to do is convert this back into a string and there's a function called joint. And if we return this, you should see that we will get the same answer as the one above. So as you can see it's again, I don't know if I'm pronouncing it right, But between four lines here, 12345. Between five lines and one line here, as you can see, Javascript functions are much more convenient. So thank you all for participating in today's challenge. And when you're ready you can go onto the next challenge.
10 Views 0 Likes 0 Comments

Use Javascript built-in functions to reverse a string

Comment
Leave a comment (supports markdown format)