Revise it. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

27th Oct 2019, 12:39 PM
Johnny Deguzman
Johnny Deguzman - avatar
2 Answers
+ 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");
27th Oct 2019, 1:30 PM
Russ
Russ - avatar
+ 1
Russ thanks mr! You nailed it!😁
27th Oct 2019, 1:41 PM
Johnny Deguzman
Johnny Deguzman - avatar