2

I am trying to create a loop to include an else statement. It works perfectly with the if statement as below but when trying to put in the else statment it either shows nothing or creates 6 loops. Im assuming I am putting the else statement in the wrong place. Can someone please explain to me a) where to put the else statement and b) the nesting criteria of the ending curly braces inside a function Heres what ive got and it works perfectly until i place the else statement in. 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")

5
  • What exactly are you trying to do? The code above works perfectly, assuming you want to check if the string parameter (in checkSport()) exists inside the sports array. Commented Jun 20, 2020 at 0:42
  • you could refactor this code into alert(sports.includes("tennis") ? 'yes ...' : 'no ...') sorry Commented Jun 20, 2020 at 0:48
  • May I know why you need else ? Commented Jun 20, 2020 at 1:04
  • you can probably tell im fairly new in my learning. I want an else statment that reads " no we dont offer that sport" If i put it in by individually checking the nodes and attaching it to the end of the last else if including all 5 nodes, it works but doesnt work in the loop, thanks for reply Commented Jun 20, 2020 at 1:18
  • if i change the sport to football i want an else statement to appear saying " sorry we dont do that sport. Its just when i put the Else statment in , it doesn't work, i have tried nesting it everywhere but where to put it and the hierachy nesting of the curly braces is my problem i think Commented Jun 20, 2020 at 1:30

3 Answers 3

3

Based on your variable names, I guess you don't have to use else in this context but you would like to end the loop/function as soon as the sport is found:

function checkSport(sportToCheck) {
  for (var i = 0; i <= sports.length; i++) {
    if (sportToCheck == sports[i]) {
      alert("yes we offer that sport");
      return; // stop the execution of the function
    } else {
      console.log("Do nothing so you don't need this else statement.");
    }
  }
  alert("No we don't offer that sport"); //  If the loop ends and cannot find any match
}
Sign up to request clarification or add additional context in comments.

1 Comment

You started your index with 0 and you make it equal to length.
1

Because your function is check() and to respect Do One Thing rule, it's better to return true or false.

There are many ways to solve your issue but I prefer find().

var sports = ["golf", "cricket", "tennis", "badminton", "squash"];

function checkSport(sportToCheck) {
  return sportToCheck === sports.find((sport) => sport === sportToCheck);
}
   
console.log(checkSport("football")); // Expect : false
console.log(checkSport("tennis"));   // Expect : true

If you want to improve @Hangindev answer you can do this:

   for (sport of sports) {
     true === (sportToCheck === sport) && alert("yes we offer that sport");
   }

Comments

-1

No need for else statements. Simply run through each possible value in the sports array, and use the return statement to pause further script execution within that function.

The function returns either true or false depending on whether the string is inside the sports array.

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");
            return true;
        }
        if (i == sports.length) {
            alert("sorry we do not offer this sport");
            return false;
        }
    }
}

checkSport("tennis")

You could use this for something like:

if (checkSport("tennis")) {
    // sport exists
} else {
    // sport does not exist
}

2 Comments

There's no reason for the second if statement, and the loop condition should be < not <=. All that's necessary is that second alert() and return false; after the loop but inside the function.
thanks , im in the early learning of javascript, im sure there is a far better way of doing what i am trying to do , i jut want to know how i could do it the way i wanted above, mainly to aid my understanding of it , thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.