I’ve always used ‘!== undefined’ to express if something exists, however in this exercise it uses ‘> -1’. Is my way ok? When is it ok to use either or? Is my reasoning of the code (in the comments) valid?
function findAgent(agentList, agentToSearchFor) {
if(agentList[agentToSearchFor] !== undefined) // if agentToSearchFor inside agentList exists
return agentToSearchFor + ' is present within Agent list' // return agentToSearchFor + ' is present within Agent list'
} // my code
function findAgent(agentList, agentToSearchFor) {
if(agentList.indexOf(agentToSearchFor) > -1) //if agentToSearchFor's index (in agentList) exists
return agentToSearchFor + ' is present within Agent list' // return agentToSearchFor + ' is present within Agent list'
} // their code