samedi 25 avril 2020

Why the output is wrong if I add an else statement

From the below code, I get the answer true, if dont include the else statement. If else, statement is included, the I'm getting a false. Why is that ?

function search(arr, item) {


    for (let i=0; i <  arr.length; i++){  
        if (arr[i] == item){ 
            return true;
        }
        else{
            return false;
        }

    }   
}

src = search([6, 2, 3, 4], 3);
console.log(src);

Aucun commentaire:

Enregistrer un commentaire