I need a piece of code that basically lets me specify the length of the array (y) and the highest index point (x), and returns an array where the index of x is equal to y.
For instance:
y = 7
x = 3
0, 1, 7, 2, 3, 4, 5, 6
Ultimately this is producing a list of numbers from 0 to 7, and then rearranging those numbers so that the largest number is 3rd in the list.
You can create an array easily using the
[]syntax:You can add items to an array using push:
So, how could you create an array from 0 to y, using a
forloop? If you can’t figure it out, mouse over the below rectangle:Okay, you have your array, now all you need to do is move the last element to the
xth position. You can do this using the very versatileArray.splice, check out the documentation.Now check your answer:
arris now the result you needed.jsFiddle.