Whats wrong on my code? in Stand in Line

Whats wrong on my code? in Stand in Line
0

#1

function nextInLine(arr, item) {
// Your code here
arr = testArr;
arr.push(item);
return arr.shift(); // Change this line
}

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

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr[2],1)); // Modify this line to test
console.log(nextInLine([5,6,7,8,9], 1));
console.log("After: " + JSON.stringify(testArr));


#2

The offending line is

arr = testArr;

#3

… and this one:

console.log(nextInLine(testArr[2],1)); // Modify this line to test

#4

isn’t working i tested


#5

Ignore testArr, use only the function arguments.


#6

Thanks everyone Problem Solved :relaxed:


#7

Stand in line

I am still having problems with this one.

function nextInLine(arr, item) {
// Your code here
arr = testArr;
arr.push(item);
return arr.shift(); // Change this line
}

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

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr[2],1)); // Modify this line to test
console.log(nextInLine([5,6,7,8,9], 1));
console.log("After: " + JSON.stringify(testArr));

Can you help?


#8

return arr.shift(); // Change this line

this line has nothing you show. nowhere to store the value that was removed. trying saving the shifted number. then returning it

edit: didn’t realize this was 2 months old. so i will write the answer as i just figure this out.

function nextInLine(arr, item) {
  // Your code here
  arr.push(item);
  var anythng = arr.shift();
  return anything;  // Change this line
}

#9

Just as lincore said - ignore the ‘Test setup’ and ignore the ‘Display Code’.
Focus on the function, the other blocks are ‘possibilities’ and presentation of a new method “JSON.stringify ()”. But if you delete all the consoles the result will give ok.

// Passing Values to Functions with Arguments Basic Javascript ( 47 )
function nextInline ( arr, num ){
// Your code here
arr.push( num ); // Basic JavaScript: Manipulate Arrays With push() Basic Javascript ( 41 )
item = arr.shift( ); // ** Manipulate Arrays With shift()** Basic Javascript ( 43 )
return item;
}
And you do not need to do anything else.
Which will be true for all the conditions requested.
If the user places an arr = “[]” and num = 5.
The function will add, through the push method, the number 5 inside the Array.
Then the function will remove the first arr number, which will be in the next step assigned to any variable (in the case ** item **), by means of the shift method.

“Finish”
I hope I have deepened the task for those who have not yet done it, who will do it, who could not and who could do it, but did not understand.


#10

My code:

function nextInLine(arr, item) {
  // Your code here
  arr.push(item);
  return arr.shift();  // Change this line
}

#11

See this;
function nextInLine(arr, item) {
// Your code here

 arr.push(item);
 var anything =arr.shift();
 return anything;

// Change this line

}

// Test Setup
var testArr = [5,6,7,8,9];

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine([], 5));// Modify this line to test
console.log(nextInLine([], 1));
console.log(nextInLine(testArr, 1));
console.log("After: " + JSON.stringify(testArr));


#12

Thank you for a problem / resolution! :slight_smile: @ Bagerian


#13

Tell us what’s happening:

What is wrong guys I don’t understand! What is suppose to do to pass this exercise?

Your code so far


function nextInLine(arr, item) {
  // Your code here
  testArr.push(item);
  var removed = testArr.shift();
  return removed;
   // Change this line
}

// Test Setup
var testArr = [5,6,7,8,9];

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 1)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
``

#14

I found it. I did wrong in the array inside the function. The problem was that I was calling the testArr array inside the function so the function wasn’t recognizing the arr argument from function. I am not sure that I explain well, if someone has an opinion please I would like to hear it.
Anyway the solution is this :

function nextInLine(arr, item) {
// Your code here
arr.push(item);
var removed = arr.shift();
return removed;
// Change this line
}

// Test Setup
var testArr = [5,6,7,8,9];

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 1)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

#15

Whats wrong with this ?

function nextInLine(arr, item) {
  // Your code here
  arr.push(item);
  arr.shift();


  return arr.shift();  // Change this line
}

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

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

I get none of the goals reached when i run the test.
ii.


#16

See that you use shift() twice? That method returns the removed item, so if with the first time you use it it returns the first item of the original array, the second time it returns the second item of the original array


#17

Thanks. Removed the first shift(), it worked


So it means that second part in the first line of these instructions “then remove the first element of the array” have to be ignored or am I getting it wrong ?
“then remove the first element of the array.” and " The nextInLine function should then return the element that was removed." are actually one line of code ?


#18

They can be one line of code, but if you do for example like the following code, they are two lines.

let a = arr.shift();
return a;

Remove the first element of the array

This tells you what your code needs to do

The nextInLine function should then return the element that was removed

This tells you what you need in front of the return statement, you need both instructions to know what to write to pass the tests