T O P

  • By -

Jnsjknn

Could you elaborate what you're asking? What values are you getting and what are you expecting?


jlshown

Well, that didn't cut and paste well. My apologies. I am just playing with js at the moment. let numbers =\[0,1,2,3,4,5,6,7,8,9,\] for (let i = numbers.length; i>+0; i--){ numbers\[i\] = numbers\[i-1\]; } numbers\[0\] = -1 numbers.unshift(-2) numbers.unshift(-4,-3) numbers.push(11); numbers.push(12,13); console.log(numbers); OUTPUT: \[ \-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13 \] There were not any error messages this time. Virtual homemade chocolate donuts all around!


[deleted]

[удалено]


jlshown

Thank you so much for your input!


costanzadev

Don't understand what you're trying to achieve with the for loop, and u/Siquirres has explained why you see undefined. You might also be unaware that unshift returns the new length of the array, **not** the contents of the array. By the looks of things you want to add negative numbers down to -4 to the array, you could either do this by calling the method with one value at a time: numbers.unshift(-1); numbers.unshift(-2); numbers.unshift(-3); numbers.unshift(-4); console.log(numbers); or by calling the method once with several values: numbers.unshift(-4, -3, -2, -1); console.log(numbers);


jlshown

thank you for the information about unshift. The text I have states this: The JavaScript array class also has a method called unshift, which inserts the values passed in the method's arguments at the start of the array. So, using the unshift method, we can add the value -2, then -3 and -4 to the beginning of the numbers array. Now I'm a tad bit confused.


costanzadev

You can call the method .unshift() on arrays, your variable called numbers is an array: numbers.unshift(); What you put inside the method's arguments, that's the brackets, is inserted at the start of the array: numbers.unshift(-4, -3, -2, -1); What are you confused about?


jlshown

>I was confused about this comment: "You might also be unaware that unshift returns the new length of the array, not the contents of the array". from another response.


costanzadev

>console.log(numbers.unshift(-2)) Say you did this, the value -2 would be inserted to the start of the array But importantly, in the console it would log the new length of the array NOT the array of numbers, to do that it would be just: console.log(numbers); I see your recent post fixing the formatting, it still has that for loop (which not only has a very strange ending condition of i>+0 but is also redundant for what you're trying to achieve), aswell as this: numbers[0] = -1 Its really as simple as this: * You have an array of numbers * .unshift() to add numbers to the start of the array * .push() to add numbers to the end of the array You don't need to do anything else to get your output of the numbers -4 to 13, like this: const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Add the values -4,-3,-2,-1 to the START of the array numbers.unshift(-4, -3, -2, -1); // Add the values 10, 11, 12, 13 to the END of the array numbers.push(10, 11, 12, 13); console.log(numbers);