Why wouldn't this solution work?

Ya, sorry about that, I got so caught up in helping you with the last argument to splice that I completely overlooked the return issue.

You read the documentation. That’s why I gave you a link to the MDN docs for splice earlier. You are aware that the splice method exists and you have a general idea of how it should work, but when it doesn’t work the way you think it should then you go to the docs and figure out why.

No worries, you still did help. I also tried two methods to the solution so that also took longer and i ended up getting slightly more frustrated. I could’ve just added n++ to my first solution

Yeah I’ve actually been pulling each up before I begin, but even now looking back I’m not seeing the return and 3rd element array rule listed, where are those two listed?

Reading documentation can be a bit of a fine art and takes some practice. The MDN docs first list the parameters. The first is the “start” index, the second is the “deleteCount” value and then the remaining are listed as:

item1, item2, ...
" The elements to add to the array, beginning from start ."

And it also lists the syntax:

let arrDeletedItems = arr.splice(start[, deleteCount[, item1[, item2[, ...]]]])

That can take some time to get used to because of all the square brackets (which mean optional) but it is telling you that any arguments after the second one have to be a comma separated list of values. The docs aren’t generally going to tell you what you can’t use but rather only what you can use as an argument. So it isn’t going to say “you can’t use an array as the third argument”.

The return value has it’s own section and the first thing it says is:

“An array containing the deleted elements.”

Again, it isn’t going to tell you that you shouldn’t use splice on the same line as your return statement because there may be circumstances when you actually want to return the deleted elements. It is up to you to know what you want to return in your function.

If the MDN docs are too technical for your liking then you can search for Array.splice and I’m sure you’ll find other pages that explain it in a slightly less technical matter. But the MDN docs are very consistent in format and will always list the parameters you can use and the return value of the function right up front, so you can get that information quickly.

Is there ever a situation that it doesn’t list out all the arguments you can use?

I see that if instead of 0 I entered 1 or 2 then it would return a result. Ok, so I guess I just had to know that and there isn’t a way to learn anything from this one to use for other non splice related functions? I’ve seen code run on one line with return and times without it, so I’m not sure how to know

I hope not. They wouldn’t be very good docs if they didn’t list everything :slight_smile:

You know by knowing what value you want to return. Again, I’ll refer you to the docs.

Syntax:

return [expression]; 

expression
The expression whose value is to be returned. If omitted, undefined is returned instead.

So the expression is evaluated first and then return returns that value.

The splice method returns an array of deleted elements, which isn’t what you wanted to return, so you couldn’t use it on the same line as the return. If you wanted to return the value of a simple arithmetic operation:

return varA + varB;

You can have that all on one line because you actually want to return the value of the expression varA + varB.

1 Like

Got it, thank you for the help!

1 Like

Please make your own thread on this topic. Thanks!