I want to create a function that works as follows:
function arraySearch(array, valuetosearchfor) {
if it finds the value in the array, it will return the key where it will find the value. If there is more than one result (more than one key) or no results at all (nothing was found), the function returns FALSE.
I found this code:
function arraySearch(arr,val) { for (var i=0; i<arr.length; i++) { if (arr[i] == val) { return i; } else { return false; } } }
and used it like this:
var resultofarraycheck = arraySearch(board, chosen); if (resultofarraycheck === false) { document.getElementById(buttonid).value; chosen = 0; }
But that does not work. When it needs to find something, it returns false instead of key (i).
How can I fix this, or what am I doing wrong?
Thank you, and I'm very sorry if my English was not clear enough.
shohamh
source share