Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
if statement meets multiple conditions but stops at first condition that matches

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 kindergarten, elementary, and high school, but skipped out of college, how do I implement this? Right now, it just stops at the first condition it meets.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

  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.

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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;
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!