Reversing an array in place - Javascript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
no mana hello what's up so today what we're gonna do is uh just a quick algorithm I sometimes I find that people a common question that's asked through the interview process is having to do with a rate manipulation reversing a raise is a big thing I actually have a video that shows like five different ways to reverse a raise and reverse strings turn them into a raise so that's a very kind of common thing but what we're gonna do today is write an impure function that reverses an array in place so a pure function is something that doesn't mutate the array so you'll bring in a bit of data and then usually you'll make some kind of clone of data and then manipulate that and this one we're not going to do that so this isn't like great best practices but it's something that is a little bit tricky if you don't actually know how to do it there's a couple moving pieces and there's a big there's a big piece of it that is useful in many other different kind of algorithms so let's talk about this for a second so for this one let's say that we wanted to reverse an array in place so we're fed an array or a string that we can turn into array but let's just keep it as an array for right now let's say that we're we're fed just one two and three and we want to reverse those two obviously be three two and one right but we want to do that on the array that's passed in and we we don't want to like push push into another array is what I'm saying so there's this idea and I remember when I first started programming years ago I was trying to figure out how to do this on my own but I didn't really understand you know I didn't know what I was doing so there's a paradigm that you can come up with about flipping elements within an array and that's temp equals first and this is in a broad sense tip temp equals first first equals last can't spell and then last equals temp so that's the if you look at it let's say that we have this one right here will equal that to a temporary variable that this store over here for example then we'll equal the first element to the last element so we'll equal this to this so now this will be 3 and then we'll equal the last element to the temporary which was actually equal to the first so then that'll be that and for this small array that would finish the algorithm but we want to make it to where it doesn't matter the size of the array that's passed in okay so let's get after this so um oh one one other thing well I'll explain it as I go so let's just say we have controverse in place right I'm gonna make spelling errors I'm gonna make spelling errors that it's passed in in a rank and then when we have that array in there we want to go ahead and declare that temporary variable that I was talking about earlier so we declare temp and then we loop through the array so let and we got to use an old school for loop for this because we're only going through half of the array and that's the part that I want to explain so I include 0 I less than our dot length divided by 2 and then I plus plus so what we're doing here is we're looping through half of the array be a link that link / to a quick thing about that I used to get confused as to whether I would have to have less than or equal to half of the array but really you only have to have it you only have to have it less than half of the array the reason is if you have let's just say five elements in the array and you go five divided by two that's gonna give you two point five you have 2.5 as your midpoint then your your loop will actually run to the third element because if you think about it less than or equal to 2.5 and they only stop at whole numbers less than or equal to two point five two will still be there but that's actually where you want to stop so it'll actually go to three so you always want to remember it's just a little gotcha less than our dot length all right cool divided by 2 so less than our dot length divided by 2 I plus plus so now let's do our array flipping that we talked about earlier so temp equals R at I so that'll be your first element in your first loop the first iteration loop and then your first and R&I is going to equal our at our dot length divided by two R naught divided by two minus one minus I I mean promote an innate that heart so R at R dot length minus one will give you the last element but then you want to go - I because each time you want to move the last element down to the left of the array so the - I will be 0 at first so it'll be last element - 0 then it will be last element - one last element - whatever I is at so that's how we do that so then our at our lane - 1 - I that's gonna equal the tip that we stored earlier right so then all we have to do is return the are ok so let's see that this works real quick and then we'll talk lots of stuff reverse in place and then a few then our and then we'll make an R up here and let's just do that 1 2 3 4 5 6 7 8 9 that's nice and easy so let's console.log is to see if it works legit scratch boom so 9 8 7 6 5 4 3 2 1 ok so if you think about it what this is doing is it's taking in an array it's actually changing that array so if we console.log you look right here if we console.log are right here actually let's declare it first so right here if we declare that array and then we console.log it right here and then we run this and then we console.log array again what we get is what you'll see is that this is the original array and then this is when we run the function and then this is the array in the glow in a global global execution context on the global scope this is what it is so we've actually mutated the array now that's you ninety-nine percent of the time you don't want to do that but in this example we were wanting to reverse the same array in place and I wanted to give this bit of logic so let's say for example that you and will just do this as kind of a one-off real quick let's say for example that you didn't want to do that that that you didn't want to mutate the array well there are a couple of ways that you can I mean obviously there's a million ways you can do it but let's go through one very easy way to we're just as an example to where you wouldn't mutate the array so let's just go on reverse and we will just name it cos reverse and then we'll pass in an array and then that'll be a whole thing and then let's let result so what we're doing right here is we're setting up another array that we're gonna actually return and then we'll just use the for let let of syntax so elem are so if you're looping through a raise its of if you're leaving through objects it's in and then we'll just get a result done shift what unsheathe does is just you know shove an element into the beginning of whatever ray that we want we're shoving it into result and then I mean basically after that this is a lot less code we can just return result now if we run the same thing to where we make an array right here and let's just let's just keep it the same array 1 2 3 4 5 6 7 8 9 we console.log array right here and then we run the we run the the function log reverse right here with the array passed in and then we'll console.log array again so we run all this what you'll see is that in this example we get what we want as the output so that's result and then on our global scope this is our array and then this is our array again so it wasn't mutated and that's what you'd normally want to do that's called pure function so if you look up pure functions it'll about it's a programming paradigm to where you take in data you don't mutate that data and you output something from that data but that it's something that isn't the exact same piece of data it's it's not mutated mutating means that you affect the data that's brought in all right so that's basically that on this reversing in place and small talking on like pure functions and things like that so I hope it helped all right take it sleazy
Info
Channel: Adam Coder
Views: 1,863
Rating: 4.9285712 out of 5
Keywords: javascript, javascript programming, code kata, algorithmic programming, algorithmic javascript, data strucgtures and algorithms, algorithms, algorithm, web development, nodeJS, node.js, taylor swift, coding interview question, front end development, code tutorial, programming tutorial, javascript tutorial
Id: 5i9tQjFXUO4
Channel Id: undefined
Length: 9min 20sec (560 seconds)
Published: Sun Sep 15 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.