Javascript Coding Challenges for Beginners Q15

Transcript

English (Auto-generated)

Hello everyone welcome back to my javascript coding challenge for beginners. So today we'll be learning manual sort because we went through a lot of javascript built in functions. So now let's turn to something more I guess something that you're more likely to see on interviews and some concepts that you might need to use in the future. So for this example as you can see we just basically have an array of numbers and we want to sort them. So if you're gonna be like why can't I just return array dot sort and if I open my terminal you should be able to solve it instantly. Yes, that's true. But for some coding challenge platforms you're not allowed to use javascript built in functions and for some coding interviews, they may not let you use it as well so you would have to manually sort it. That's why we're here today to manually sorted because I did get this question for an interview question. So I guess pasta video and come up with their own solution and when you're ready we can tackle the solution together. So now that you're ready I guess let's work on the solution together. So first of all because we have an array of random numbers and we want to sort them we will need a loop. So for that I equals zero I is less than three dot link I plus plus. And because we want to start off with one number and we also want to check if the next number is less or greater than the previous one. We would need a second loop. So for what J equal hypothesis one. Because for the second loop, we don't want to start at zero again because we don't want to compare with ourselves. There's no point doing that. So I want to start with the next one. So whatever I is we want to do I plus one. So we just want to compare the rest and then jay is less than railing J plus plus. Now here's the thing we want to compare to see that which one is a smaller number. So if ray I so I forgot that if ray I is greater than array. Okay. What we wanna do is first we want to store our array I into a temporary variable. So let's temp equal array I You will see why we need this later. And then now we wanna make array I equal to arrange a because clearly a ray J is less than a ray I. So you want a ray J to be in the position of array? So array I equal array. Okay. However, because we stored our ai in temporary, we did not lose the value of ray because we're storing it somewhere. So now, because of ray J is a ray I we're gonna do a ray J, Ray J. I So this makes sense. See we can return our All right. And if we check in the terminal, let's see that. Did I type something wrong? Oh yeah, exactly, I J equal. Alright. But I started the other values. So let's run this again. Clear node 15 plot. J S Okay. Like having typing issues today. Eight. I am so sorry. I'm like, I'm like so out of it today. So as you can see, we still get our whole array sorted. But this is the manual source. All we did was replacing temporary numbers into one place and then we're equaling array I into raging and then we're signing a ray J back to temporary value so we don't lose array I So this makes sense to you congrats because you learn important concepts and that is we can store variables into another variable so that we don't lose it. So thank you all for watching. Um Come back for the next challenge when you're ready.
67 Views 0 Likes 0 Comments

Manual Sort

Comment
Leave a comment (supports markdown format)