I think its a bug my code is adding 1 to some values I don't know what's happening

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function nextInLine(arr, item) {
// Only change code below this line
testArr[4]=item
return arr[0] |item;
// Only change code above this line
my code is only getting the value of item not arr , I tried to solve but still not working properly. 
console.log(JSON.stringify([2], 1))
console.log(nextInLine([5,6,7,8,9], 1))
these two lines are causing problem first should return 2 but it returns [2] sometimes 3 and the second is working but doesn't making sense to me how ,cause it should return 5 according to my understanding return arr should be arr[0][0] but its working with arr[0]
also console.log(nextInLine(testArr, 10)) it should return 10 but it returns 11 

}

// Setup
const testArr = [1, 2, 3, 4, 5];
console.log(nextInLine([], 5))
console.log(nextInLine([], 1))
console.log(JSON.stringify([2], 1))
console.log(nextInLine([5,6,7,8,9], 1))
console.log(nextInLine(testArr, 10))

// 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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Safari/605.1.15

Challenge: Stand in Line

Link to the challenge:

Why are you hard coding 4 and 0 here?

cause it should replace 4th index with item

That’s not the instructions though. The instructions say to put the item at the end and remove the first element.

I did that but instructions are also different have a look please.

nextInLine(, 5)` should return a number.

nextInLine([], 1) should return 1

nextInLine([2], 1) should return 2

nextInLine([5,6,7,8,9], 1) should return 5

After nextInLine(testArr, 10) , testArr[4] should be 10

You did not follow the instructions.

Your code

  1. replaces the element at index 4 with item
  2. returns the element at index 0 without removing it

You need to

  1. put the item at the end of the array
  2. remove the first element of the array

In two recent previous challenges you covered methods that do these two tasks.
Your code must work for any array.

1 Like

I did this
arr.push(item)
arr.shift()
what else should I do?

What is your full new code?

function nextInLine(arr, item) {
  // Only change code below this line
  arr.push(item)
  arr.shift()
  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));

Here you are returning item but you need to remove the element that you removed from the array

1 Like

arr.push(item)
const element = arr.shift()
return element ;
thank you I worked now, but I don’t understand how its working :frowning:

Well, what does each of these methods do?

Push:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

The push() method adds one or more elements to the end of an array and returns the new length of the array.

Shift:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift

The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

Those are exactly the two tasks requested by this challenge.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.