0
Revise it.
function game(find) { var planets = ["Earth", "Mars", "Saturn", "Neptune"]; var le = planets.length; for(var i = 0; i <= le; i++) { if( planets[i] === find ) { console.log("Yes we have this planet"); break; } else { console.log("No we don't sorry"); break; } } } var find = prompt("Enter planet name: "); game(find); Without "break;" it'll pick up its pair but before that it will run the "ELSE" statement. Please try and see some Logical errors.
2 ответов
+ 2
The problem is that you are looping over your planets array and checking whether each one matches the input. But as soon as one doesn't match, it says "no, we don't have that one".
You need to loop over all of them and only print "no" after all have been checked. See below.
for (var i = 0; i < planets.length; i++) {
if (planets[i] === find) {
console.log("Yes we have this planet");
return;
}
}
//if planet is not found, we will get to here.
console.log("No we don't sorry");
+ 1
Russ thanks mr! You nailed it!😁