Freecodecamp: It appears my code delivers what the lesson requires, yet it indicates an error.

Created on 25 Jul 2016  路  9Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge Stand in Line has an issue.
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.

My code:

function nextInLine(arr, item) {
  arr.push(item);
  return arr.splice(0, 1);
}

// Test Setup
var testArr = [];

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

It appears my code delivers what the lesson requires, yet it indicates an error.

help wanted

Most helpful comment

.splice will return an array, while the tests are looking for a number.
Hence with although your code prints the correct output it fails the test.

I feel we should add a testcase stating:

nextInLine should return a number.

All 9 comments

.splice will return an array, while the tests are looking for a number.
Hence with although your code prints the correct output it fails the test.

I feel we should add a testcase stating:

nextInLine should return a number.

The same code will still appear to return a number while failing that test, so I'm not sure that addresses the issue.

Can we have the output show an array containing a single number element as an array rather than a number?

Yeah it's possible, but we gotta hack the tail code to detect if it's an array and then append the [ and ], yet this might get buggy with different solutions.

Hello, I'm new here, but I want to share my code that also doesn't work:

function nextInLine(arr, item) {
  testArr.push(item);
  return testArr.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, 1)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

In this case .shift actually returns a number, but still I can't pass "nextInLine([5,6,7,8,9], 1) should return 5" check.

@dendunn I believe in your case, the reason the tests aren't passing are because you need to use arr instead of testArr. I know this can seem confusing because the array in the Test Setup is called testArr.

Code works fine. Listen what @dhcodes said above me.

screenshot at 2016-10-06 11 41 02

Hi, I would like to help fix this. I understand that the solution is to add a test which checks that the result is actually a number.
How do I add a test? I need to work on the "Stand In line" page?

I can't seem to get this one to work either. I keep getting an error message, TypeError: arr.shift is not a function.

`function nextInLine(arr, item) {
arr = arr.push(item);
var nextUp = arr.shift();

return nextUp; // 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));`

@knappsacks You've set arr to arr.push(item), which returns an integer representing the new length of the array. arr now has the value of 6, and therefore arr.shift is not a function. The key idea is that arr.push(item) modifies the array itself, and doesn't need the assignment.

Was this page helpful?
0 / 5 - 0 ratings