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");
}
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 >= 601 && credit <= 657){
console.log(credit)
document.write("POOR");
}
else if (credit >= 658 && credit <= 719){
console.log(credit)
document.write("FAIR");
}
else if (credit >= 720 && credit <= 780){
console.log(credit)
document.write("GOOD");
}
else {
(credit >= 781 && credit <= 850);
console.log(credit)
document.write("EXCELLENT");
}
ps: Code need the 'and' clause (&&) between the two values to work. Because, (True and False) = (False and True) = (False and False) = False. Its only true when when both members are true!
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");
}
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");
}