Step 57 “Step Inline” is impossible for me to pass

Hello there, happy it worked out. Storing arr.push() in a variable is not useful because this method changes the array on which it is used. So you already have that array.
You could store arr.shift() in a variable as you have done. But since you are not really doing anything with it, you could have just returned arr.shift().
Anyway well done to keep at it.

Yes, what @Slimattcode said. But just to be clear, push returns the size of the new array, not needed in this case.

@Spychu1993 I would advise putting in some console.log statements in the function to see what is happening (like I showed above) and sending your function some different values.

1 Like

That is what you removed with shift. When in doubt, check the documentation:

Ok, so you wrote:

var arr = [1, 2, 3, 4, 5, 6];
console.log(‘arr before’, arr);
arr.push(7);
console.log(‘arr after push’, arr);
arr.shift();
console.log(‘arr after shift’, arr);
return item;

and I am not exactly sure why there’s “arr.shift();” or “arr.push();” between console.log and why there is (‘arr after shift’, arr); and (‘arr after push’, arr);

I just thought the only thing I can call by console.log is

console.log(nextInLine);  or  console.log(item/array);

I see on the bottom

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

and that made me confused.

BTW I pasted

console.log(‘arr before’, arr);

and it shows error. Is it just example that you wrote and I should adjust it to my particular case?

May I also ask why

count

is not defined? It is colored gray
count

Only when I call:

console.log(nextInLine);

Then it shows the result of what happened. So what more can I put in console.log? What more can I call in this particular case and why?

Where did you paste those lines? Inside of the function definition or outside?

Sorry sir, which lines exactly? I wrote a bunch above :sweat_smile:

This right here? Where did you put it? How did you get an error? What error did you get?

You should see something like

arr before [ 1, 2, 3, 4, 5, 6 ]
arr after push [ 1, 2, 3, 4, 5, 6, 7 ]
arr after shift [ 2, 3, 4, 5, 6, 7 ]

When I put it like this:

function nextInLine(arr, item) {
  // Only change code below this line
const count = arr.push(item);
  const count1 = arr.shift();

  return count1;
  // Only change code above this line
}

var arr = [1, 2, 3, 4, 5, 6];
console.log(‘arr before’, arr);
arr.push(7);
console.log(‘arr after push’, arr);
arr.shift();
console.log(‘arr after shift’, arr);
return item;

// 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));

I get

SyntaxError: unknown: Unexpected character ‘‘’. (11:12)
9 |
10 | var arr = [1, 2, 3, 4, 5, 6];
11 | console.log(‘arr before’, arr);
| ^
12 | arr.push(7);
13 | console.log(‘arr after push’, arr);
14 | arr.shift();

Like this:

function nextInLine(arr, item) {
  // Only change code below this line
const count = arr.push(item);
  const count1 = arr.shift();

  return count1;
  // 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));
var arr = [1, 2, 3, 4, 5, 6];
console.log(‘arr before’, arr);
arr.push(7);
console.log(‘arr after push’, arr);
arr.shift();
console.log(‘arr after shift’, arr);
return item;

I get

SyntaxError: unknown: Unexpected character ‘‘’. (20:12)
18 | console.log("After: " + JSON.stringify(testArr));
19 | var arr = [1, 2, 3, 4, 5, 6];
20 | console.log(‘arr before’, arr);
| ^
21 | arr.push(7);
22 | console.log(‘arr after push’, arr);
23 | arr.shift();

Like this:

function nextInLine(arr, item) {
  // Only change code below this line
const count = arr.push(item);
  const count1 = arr.shift();

  return count1;
  var arr = [1, 2, 3, 4, 5, 6];
console.log(‘arr before’, arr);
arr.push(7);
console.log(‘arr after push’, arr);
arr.shift();
console.log(‘arr after shift’, arr);
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));

I get

SyntaxError: unknown: Unexpected character ‘‘’. (8:12)
6 | return count1;
7 | var arr = [1, 2, 3, 4, 5, 6];
8 | console.log(‘arr before’, arr);
| ^
9 | arr.push(7);
10 | console.log(‘arr after push’, arr);
11 | arr.shift();

Or should I just copy only console.log lines?

those words ‘before’ and ‘after’ I know it only from CSS styling. I am not sure what does it mean and how to use it in JS. I will do some reading about it.

You have to use single '' or double "" quotes.

1 Like

Right, you have “smart quotes”. Notice that they are angled towards the text. JS only likes “dumb” or “straight” quotes. Notice the difference in quotes here:

‘arr before’

and here

'arr before'

Do you see the subtle difference in shape?

Unfortunately most people like smart quotes so sometimes word processors or even cut and paste will convert them.

Yes, I see the difference.
So I put double quotes like this:

   console.log("arr before", arr);
  arr.push(7);
  console.log("arr after push", arr);
  arr.shift();
  console.log("arr after shift", arr);
  return item;

and I placed it under “}” which ends nextInLine function and it shows:

SyntaxError: unknown: ‘return’ outside of function. (16:2)
14 | arr.shift();
15 | console.log(“arr after shift”, arr);
16 | return item;
| ^
17 |
18 |
19 |

Just to be clear, the issue isn’t single or double quotes - double quotes also have smart and dumb versions. This issue is that JS (and I think all programming languages) only want dumb quotes. It is a sneaky problem sometimes.

As to testing, I don’t know why you are using that code. Test it in your working function. Last I saw you had this:

function nextInLine(arr, item) {
  const count = arr.push(item);
  const count1 = arr.shift();
  return count1;
}

As mentioned, you don’t need the variable count so let’s get rid of that.

function nextInLine(arr, item) {
  arr.push(item);
  const count1 = arr.shift();
  return count1;
}

It was also pointed out that you don’t need the count1 variable - you could return that directly - but that makes it easier to log out so let’s keep it in for now.

Now we want to add in log statements.

function nextInLine(arr, item) {
  console.log("in function - arr:", arr);
  console.log("in function - item:", item);
  arr.push(item);
  console.log("in function - arr after push:", arr);
  const count1 = arr.shift();
  console.log("in function - arr after shift:", arr);
  console.log("in function - element removed from array, to be returned:", count1);
  return count1;
}

then I would add code to feed it so I can see what is happening:

function nextInLine(arr, item) {
  console.log("in function - arr:", arr);
  console.log("in function - item:", item);
  arr.push(item);
  console.log("in function - arr after push:", arr);
  const count1 = arr.shift();
  console.log("in function - arr after shift:", arr);
  console.log("in function - element removed from array, to be returned:", count1);
  return count1;
}

const testArr = [1, 2, 3];
const itemToAdd = 127;

console.log("array before function:", testArr); 

const returnValue = nextInLine(testArr, itemToAdd);

console.log("array after function:", testArr); 
 
console.log("returned value:", returnValue);

Then change around your test values:

const testArr = [1, 2, 3];
const itemToAdd = 127;

to see what is happening.

1 Like

Ok, so a friend showed me I can do this:

arr.push(item);
return arr.shift();

Just two lines works very well.

@kevinSmith

As to testing, I don’t know why you are using that code. Test it in your working function.

I have troubles with definitions here. What is that “code I am using” and what is my working function? Isn’t the working function:

function nextInLine(arr, item) {

}

?

Next, I am really overhelmed by that:

function nextInLine(arr, item) {
  console.log("in function - arr:", arr);
  console.log("in function - item:", item);
  arr.push(item);
  console.log("in function - arr after push:", arr);
  const count1 = arr.shift();
  console.log("in function - arr after shift:", arr);
  console.log("in function - element removed from array, to be returned:", count1);
  return count1;
}

const testArr = [1, 2, 3];
const itemToAdd = 127;

console.log("array before function:", testArr); 

const returnValue = nextInLine(testArr, itemToAdd);

console.log("array after function:", testArr); 
 
console.log("returned value:", returnValue);

I really don’t where do those lines come from. I can’t comprehend it. Maybe I want to understand too much? I think my problem is I lack of knowledge about what are the possibilities and meaning of functions, arrays, variables etc.

Yes, Slimattcode already mentioned that:

But since you are not really doing anything with it, you could have just returned arr.shift().

But the point was that by saving the return of the shift we could log it out in the function. But yes, for my final version I would not have that third line. In actuality, there are shorter solutions, that’s not important right now.

I have troubles with definitions here. What is that “code I am using” …

The working code that you had offered above, put into the function. In more recent posts you seem to have reverted to non-working code.

… and what is my working function? Isn’t the working function:

function nextInLine(arr, item) {

}

No, that is a function, but it is not working in the sense that it does not do what it needs to do, it does not meet the requirements of the challenge. If we need a function that does nothing and returns undefined, then sure, I guess that is a “working function”. But we needed a function that manipulated an array and returned a value. In that sense, it is not working.

Next, I am really overhelmed by that:
… [code example]

I went to here you had working code, this:

const count = arr.push(item);
const count1 = arr.shift();
return count1;

That is working code, but does not meet the requirements of the challenge because it does not appear to be in the function. You thought you’d said you’d succeeded so I assumed it got put in the function:

function nextInLine(arr, item) {
  const count = arr.push(item);
  const count1 = arr.shift();
  return count1;
}

I assumed that you’d done that at some point in order to pass.

I then removed the unnecessary count variable.

function nextInLine(arr, item) {
  arr.push(item);
  const count1 = arr.shift();
  return count1;
}

I then added some test variables

function nextInLine(arr, item) {
  arr.push(item);
  const count1 = arr.shift();
  return count1;
}

const testArr = [1, 2, 3];
const itemToAdd = 127;

nextInLine(testArr, itemToAdd);

Then I added in a bunch of console.log statements so you could see what was happening as the code ran.

function nextInLine(arr, item) {
  console.log("in function - arr:", arr);
  console.log("in function - item:", item);
  arr.push(item);
  console.log("in function - arr after push:", arr);
  const count1 = arr.shift();
  console.log("in function - arr after shift:", arr);
  console.log("in function - element removed from array, to be returned:", count1);
  return count1;
}

const testArr = [1, 2, 3];
const itemToAdd = 127;

console.log("array before function:", testArr); 

const returnValue = nextInLine(testArr, itemToAdd);

console.log("array after function:", testArr); 
 
console.log("returned value:", returnValue);

But maybe that last step would be better if you do it yourself. You can start back with this:

function nextInLine(arr, item) {
  arr.push(item);
  const count1 = arr.shift();
  return count1;
}

const testArr = [1, 2, 3];
const itemToAdd = 127;

nextInLine(testArr, itemToAdd);

and add in your own console.log statements and see how it works. Good coders are good engineers. And one of the qualities of a good engineer is that they like to take things apart and see how they work. These are also important skills for debugging - and every professional coder needs to be good at debugging.

You have working code. See if you can figure out why it works.

1 Like

Thank you for your time Kevin, I will rethink it and work on it. I do want to know how everything works, not just the outcome.

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