Reset the variable's value when the search function is called. Also, the default value of the foundIndex couldn't be "-1". Maybe the array has a -1 index.