Please help with the Stand In Line Javascript exercise

Please help with the Stand In Line Javascript exercise
0.0 0

#1

Hello community, I’ve spent hours trying so many variations and I am stuck. I am hoping for some suggestions on what’s wrong with my code (not necessarily the solution as I need only a few pointers )

There are four required criteria to complete the exercise; yet my code passes off as completing #1,#2, & #4 of the items required (even though I have only added code that should address two of the four requirements)

#1 nextInLine([], 1) should return 1
#2 nextInLine([2], 1) should return 2
#3 nextInLine([5,6,7,8,9], 1) should return 5
#4 After nextInLine(testArr, 10), testArr[4] should be 10

Code output:
Before:[1,2,3,4,5]
1
2
After:[3,4,5,1,10]

Here is the code:

function nextInLine(arr, item) {
       testArr.push(item); //pushes 1 onto end of array and returns value of that location
       item=testArr.shift(item); //removes 1st element --> yet "10" is pushed to the end 
      
 return item;  
}

// 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(nextInLine(testArr, 10)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));


Here is the solution that member fuqted helped me resolve the issue I was having:
function nextInLine(arr, item) {
arr.push(item); //pushes 1 onto end of array and returns value of that location
// arr.shift();
item=arr.shift(); //removes 1st element --> yet “10” is pushed to the end

   return item;  

}

// 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(nextInLine(testArr, 10)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));


#2

It’s not effecting anything, but you’re using shift wrong. Shift doesn’t take any arguments.

Your code isn’t working because you’re modifying testArr instead of arr.

You should use www.repl.it and do challenges on www.codewars.com


#3

Hey thanks for the tips. I’ll revise my code to see if I can get it working. Also, thanks for the link; I’ll take a look at it too.


#4

this is repl.it

https://repl.it/languages/javascript

It’s just the easiest thing to use I’ve come across. Tutorial / challenge IDE’s typically suck and you should be able to test things as easily as possible. That’s how you learn.

Codewars has tons of challenges, and at different skill levels. Whereas FCC challenges like to cover one thing and like you to solve a challenge in a particular way, you’re onto the next thing before it’s instilled as a new skill - codewars will let you gradually learn what you choose to learn.

If you’re past the tribute page I’d suggest giving FCC a break and sticking with codewars for a week or so.


#5

Thank you fuqted. You solved the issue I was having… How do I go about making sure you get the brownie points (I think that’s what they’re called so that you get recognition you solved an issue)?


#6

This is how I did mine. Hope it helps. :slight_smile:

WARNING! Spolier for “Stand in Line”

Summary


#7

Hey Vicky01 thanks for the reply and eventually I ended up having the same solution too (I had initially used the global testArr variable instead of the local arr variable; but once I corrected that i was able to pass the exercise). That was great you were able to share. Thank you so much.
Dan