Javascript Coding Challenges for Beginners Q7
Transcript
English (Auto-generated)
Hello everyone Welcome back to my javascript coding challenge course for beginners today, we will be using another common javascript built in function to solve our problem for this example we want to shift the ray to the left by X number of times. So we're given an array an X number of times that we want to shift it. So what they mean by that is when they shifted. So first say if the number is one we want to shift one to the left so one will now go to the end of the array. If that makes sense, then please come up with your solution and when you're ready we can tackle the solution together. So now that you're ready, let's get to it. Mhm. So because we have to shift theory by X number of times it's better to use a for loop. So we can go through each iteration. So let I equal zero. We want to start at zero and I is less than X because here were given X number of times. So we want I to iterate less than two times. Mhm and high plus plus and now we want to shift each one to the end. So in javascript there is a built in function called shift. I know right? So let I guess some shift equal to r dot shift. And then there's also another function in javascript called push. So what push does is they push us the argument that you put inside into the end of the ring. So shift removes the first index out of the array and push pushes anything back to the end of the A ring. So in this case we want to push what we shifted to the end. So let's do our about push shift and we can return are. So let's see if this works will open our terminal. And as you can see if we shifted once, we should see 23451. But we shifted twice. We should see 34512. So let me just go through it one more time. If you don't know what shift is the shift just removes the first index in the array and push pushes anything back to the end of the rate. So if you're pushing shift, you're basically pushing one back to the end. And if you go to the next iteration, you're removing number two now and you're pushing number two back to the end of the rig. So thank you all for watching. When you're ready. We can go on to the next challenge. Mhm, mm hmm.