Struggling to create a loop that includes an else statement. The if statement works fine, but when adding the else statement, it either shows nothing or creates multiple loops. It seems I may be placing the else statement in the wrong spot. Can someone explain where to correctly place the else statement and the nesting criteria for ending curly braces within a function? Here is my code that works well until I add the else statement. Thanks!
var sports = ["golf", "cricket", "tennis", "badminton", "squash"];
function checkSport(sportToCheck) {
for (var i = 0; i <= sports.length; i++) {
if (sportToCheck == sports[i]) {
alert("yes we offer that sport");
}
}
}
checkSport("tennis")