Skip to content
Discussion options

You must be logged in to vote

You just need to do index < arr.length you added a -1 for no reason.

To meet it seems you could just rewrite this as

return arr[arr.indexOf(item)+1]

For the same effect. Is it possible to pass in an item that ISNT present? If not, then you never need to check for -1.

If it's just that it might be undefined then you can just do

return item && arr[arr.indexOf(item)+1]

And if the index if out of bounds, it returns undefined, which is already what your function does if the index is too high.

If you are looping over these in Alpine, you can get the index in the for loop and directly use that instead of doing this extra loop to get the index.

Or even use a stored iterator instead.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Ravenna
Comment options

@ekwoka
Comment options

Answer selected by Ravenna
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants