As I delve into learning JavaScript through Eloquent Javascript, I encountered an error in one of the chapters that has me stumped. The error message "Cannot read property 'indexOf' of undefined" is appearing with this line of code:
return journal.events.indexOf(event) != -1
Furthermore, I am puzzled about how that line of code operates. Isn't indexOf supposed to return the first position of occurrence of a specified value (in this case, event)? However, the book shows that
results in either true or false.return journal.events.indexOf(event) != -1;
var journal = []; function addEntry(events, didITurnIntoASquirrel) { journal.push({ events: events, squirrel: didITurnIntoASquirrel }); } addEntry(["work", "touched tree", "pizza", "running", "television"], false); addEntry(["work", "ice cream", "cauliflower", "lasagna", "touched tree", "brushed teeth"], false); addEntry(["weekend", "cycling", "break", "peanuts", "beer"], true); function hasEvent(event, entry) { return entry.events.indexOf(event) != -1; } console.log(hasEvent("pizza", journal));