Home > Enterprise >  If/Else Statement Failing
If/Else Statement Failing

Time:04-02

I'm trying to write a simple script that will look at a credit number rating and show the corresponding text rating. The if-else statement breaks when I get to the second else/if statement and I cannot figure out why so anything over 601 just reads "POOR".

credit = 690;

if (credit <= 600) {
    document.write("VERY POOR");
} 
else if (credit >= 601 || credit <= 657){
    document.write("POOR");
}
else if (credit >= 658 || credit <= 719){
    document.write("FAIR");
}
else if (credit >= 720 || credit <= 780){
    document.write("GOOD");
}
else {
    (credit >= 781 || credit <= 850);
    document.write("EXCELLENT");
}

CodePudding user response:

You should replace the || with &&.

|| means OR, meanwhile && means AND. You need to check if credit is greater than 601 AND lower than 657.

Try this way:

credit = 690;

if (credit <= 600) {
    document.write("VERY POOR");
} 
else if (credit >= 601 && credit <= 657){
    document.write("POOR");
}
else if (credit >= 658 && credit <= 719){
    document.write("FAIR");
}
else if (credit >= 720 && credit <= 780){
    document.write("GOOD");
}
else {
    (credit >= 781 && credit <= 850);
    document.write("EXCELLENT");
}

CodePudding user response:

The problem of your code is that when you code:

else if (credit >= 601 || credit <= 657){
    document.write("POOR");
}

this is true, so it will say POOR, because you are dealing with or clause witch means (True or False) = (False or True) = True:

The solution is removing the first argument:

let credit = 690;

if(credit <= 600) {
    console.log(credit)
    document.write("VERY POOR");
} 
else if (credit <= 657){
    console.log(credit)
    document.write("POOR");
}
else if (credit <= 719){
    console.log(credit)
    document.write("FAIR");
}
else if (credit <= 780){
    console.log(credit)
    document.write("GOOD");
}
else {
    (credit <= 850);
    console.log(credit)
    document.write("EXCELLENT");
}

ps: code will not work perfectly you need to put 'and' clause (&&) between the two values

CodePudding user response:

it seems you have confused AND and OR boolean condition, your condition :

else if (credit >= 601 || credit <= 657){
    document.write("POOR");
}

catches everything that superior to 601, because that is literally what you asked it to do. If you want a range check, your should write :

else if (credit >= 601 && credit <= 657){
    document.write("POOR");
}

and so on for each of you else if statement. :)

CodePudding user response:

in these cases you must use the &&

&& Operator will define that the two values ​​need to be true, OR Operator will define that to execute, only one value is true.

you can read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND

    let credit = 750;

if (credit <= 600) {
    document.write("VERY POOR");
} 
else if (credit >= 601 && credit <= 657){
    document.write("POOR");
}
else if (credit >= 658 && credit <= 719){
    document.write("FAIR");
}
else if (credit >= 720 && credit <= 780){
    document.write("GOOD");
}
else {
    (credit >= 781 && credit <= 850);
    document.write("EXCELLENT");
}
  • Related