Home > Back-end >  if statement meets multiple conditions but stops at first condition that matches
if statement meets multiple conditions but stops at first condition that matches

Time:11-07

I am using an if statement and trying to make sure that it stops when it meets most of the conditions that it can. For example if I have the following code:

  const determineLevel = () => {
    const wentToKindergarten = true;
    const wentToElementary = true;
    const wentToHighSchool = true;
    const wentToCollege = false;

    if (wentToKindergarten) {
      return 1;
    }

    if (wentToKindergarten && wentToElementary) {
      return 2;
    }

        if (wentToKindergarten && wentToElementary && wentToHighSchool) {
      return 3;
    }

 
     if (wentToKindergarten && wentToElementary && wentToHighSchool && wentToCollege) {
      return 4;
    }

    return;
  };

In this example, if a student went to kidergarten, elementary, and highschool but skipped out of college, how do I implement this? Right now, it just stops at the first condition it meets. Thanks!

CodePudding user response:

  const determineLevel = () => {
    const wentToKindergarten = true;
    const wentToElementary = true;
    const wentToHighSchool = true;
    const wentToCollege = false;
    var returnme=0;


    if (wentToKindergarten) {
      returnme= 1;
    }

    if (wentToKindergarten && wentToElementary) {
      returnme= 2;
    }

        if (wentToKindergarten && wentToElementary && wentToHighSchool) {
      returnme= 3;
    }

 
     if (wentToKindergarten && wentToElementary && wentToHighSchool && wentToCollege) {
      returnme= 4;
    }

    return returnme;
  };

Edit: Or you might prefer to place the conditionals in reverse order so that hardest to satisfy is checked first. Whatever you prefer.

CodePudding user response:

The return statement finishes the function, a function will not execute past the first return statement that it finds. So a better way to do your code would be something like:

const determineLevel = () => {
    var returnNumber;
    const wentToKindergarten = true;
    const wentToElementary = true;
    const wentToHighSchool = true;
    const wentToCollege = false;

    if (wentToKindergarten) {
      returnNumber = 1;
    }

    if (wentToKindergarten && wentToElementary) {
      returnNumber = 2;
    }

        if (wentToKindergarten && wentToElementary && wentToHighSchool) {
      return = 3;
    }

 
     if (wentToKindergarten && wentToElementary && wentToHighSchool && wentToCollege) {
      returnNumber = 4;
    }

    if(returnNumber > 0)
    {
        return returnNumber;
    }
    return;
  };

This way you check through all scenarios then return whatever number you need at the end.

CodePudding user response:

Your code was not working for your logic because, you are returning a value inside each if condition check, which will cause the function to stop execution after the first condition becomes true.

You can change the if condition to else-if conditions.

Or you can have a variable to keep track off,

const determineLevel = () => {
const wentToKindergarten = true;
const wentToElementary = true;
const wentToHighSchool = true;
const wentToCollege = false;
let condition;
if (wentToKindergarten) {
  condition= 1;
}

if (wentToKindergarten && wentToElementary) {
  condition= 2;
}

if (wentToKindergarten && wentToElementary && wentToHighSchool) {
  condition= 3;
}


 if (wentToKindergarten && wentToElementary && wentToHighSchool && wentToCollege) {
  condition= 4;
}

 return condition;
};

CodePudding user response:

1 - in the reverse order.

if ( wentToKindergarten 
   && wentToElementary 
   && wentToHighSchool 
   && wentToCollege     )   return 4;

if (wentToKindergarten 
   && wentToElementary 
   && wentToHighSchool  )   return 3;

if ( wentToKindergarten 
   && wentToElementary  )   return 2;

if (wentToKindergarten  )   return 1;

2 - with some else.

if (wentToKindergarten) 
  if (wentToKindergarten)
    if (wentToHighSchool)
      if (wentToCollege)  return 4
      else                return 3
    else                  return 2
  else                    return 1
else  return          
  • Related