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));
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));
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.
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));
``
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));
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.
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
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 ?