Sorry if there is something wron with my english :( I' ve started learning Java Script and there alot of things that make me confuse I'm going to loop through an array and check if there is any number that I want to find. I tried alot and find out that it's because the position of 'return'. In my understand it's should be the second code to be true, but it's not. can anyone help me please. Here is my code that works:
//Find 3 in Array
var a = [1,2,3,4];
function findX(){
for (let i = 0; i < a.length; i++){
if (a[i] == 3){
return true;
}
}
return false;
}
console.log(findX());
Here is my code that did not work:
var a = [1,2,3,4];
function findX(){
for (let i = 0; i < a.length; i++){
if (a[i] == 3){
return true;
}
return false;
}
}
console.log(findX());// return false with any number