I return some of the tests correct but I am not sure why some of my answers come up undefined. I feel like my solution should work with both arrays and strings. Any advice would be appreciated!
Here is the problem: Given a sequence of items and a specific item in that sequence, return the item immediately following the item specified. If the item occurs more than once in a sequence, return the item after the first occurence. This should work for a sequence of any type.
When the item isn't present or nothing follows it, the function should return nil in Clojure and Elixir, Nothing in Haskell, undefined in JavaScript, None in Python.
Examples provided:
nextItem([1, 2, 3, 4, 5, 6, 7], 3) // 4
nextItem("testing", "t") // "e"
My code:
function nextItem(xs, item) {
for(let i = 0; i < xs.length; i++){
if(xs[i] === item){
return xs[i + 1]
}
}
}
console.log(nextItem([1, 2, 3, 4, 5, 6, 7], 3)) // 4
console.log(nextItem("testing", "t")) //e