Adding and removing items from an array

Tell us what’s happening:
Describe your issue in detail here.
how do i get the function to return 5, its the one step am struggling with, when i try to make modifications to get it to work i them mess up the previous progress

  **Your code so far**
function nextInLine(arr, item) {
// Only change code below this line
arr = arr.push(item)

testArr.shift()

return arr;

// Only change code above this line
}

// Setup
const testArr = [1, 2, 3, 4, 5];

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

Challenge: Stand in Line

Link to the challenge:

Do you know what the return value of push is? This doesn’t do what you think it does.

You should never reference the global variables inside of this function. You need to only use arr and item.

You need to return the removed item, not the array.

not gonna lie this is super confusing, all i know is i can use the push function to add an item to the end of an array but the return value that i can’t seem to figure out

Take my comments one at a time.

  1. Can you fix the fact that push returns a number, so calling arr = arr.push(item) deletes your array?

alright, ive switched that to, /* let addToArr = arr.push(item) */

You don’t need the return value. You didn’t need it on this challenge:https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/manipulate-arrays-with-push

Anywho, can you do the next thing

  1. You should never reference the global variables inside of this function. You need to only use arr and item.

i’ve fixed the global variable thing, actually the challenge am working on is the “stand in line” …

I know which challenge you are working on. I said

You don’t need the return value. You didn’t need it on this challenge

Do you remember doing that challenge in the past? Do you see anywhere in that challenge where you used the return value from push?

Looking at previous challenges can help refresh your memory when you make mistakes.


What is your current code?

function nextInLine(arr, item) {
  // Only change code below this line
  let addToArr = arr.push(item)

  //return item;
  // Only change code above this line
}

// Setup
const testArr = [1, 2, 3, 4, 5];

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

You fixed the fact that your code references global variables, but now you need to add some code to support this requirement:

remove the first element of the array.

What array method removes the first element?

function nextInLine(arr, item) {

// Only change code below this line

let addToArr = arr.push(item)

let removeFromArr = arr.shift()

return removeFromArr;

// Only change code above this line

}

// Setup

const testArr = [1, 2, 3, 4, 5];

// Display code

console.log("Before: " + JSON.stringify(testArr));

console.log(nextInLine(testArr, 6));

console.log("After: " + JSON.stringify(testArr));

finally…thanks alot :+1:t6:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

amazing, i’ll remember that next time i post