why my else if statement is not working in this case?

 class GpaCalc{
    constructor(firstName,math,phy,chem){
        this.firstName = firstName;
        this.math = math;
        this.phy = phy;
        this.chem =chem;
    }

    result(){
        let totalNum = this.math + this.phy + this.chem;
        let percentage = (totalNum * 100) / 300;
        if( this.firstName === ``){
            console.log("Please Enter username.")

         //Problem is in this line below
        } else if(this.math > 40 || this.phy < 40 || this.chem < 40){
            console.log("You have Failed!")

        } else if (percentage >= 80){
            console.log(`${this.firstName}, You got A+`)
        } else if (percentage >= 70){
            console.log(`${this.firstName}, You got A`)
        } else if (percentage >= 65){
            console.log(`${this.firstName}, You got A-`)
        } else if (percentage >= 60){
            console.log(`${this.firstName}, You got B`)
        } else if (percentage >= 50){
            console.log(`${this.firstName}, You got C`)
        } else if (percentage >= 40){
            console.log(`${this.firstName}, You got D`)
        } else {
            console.log(`${this.firstName}, You have Failed!`)
        }
    }
}

    const student1 = new GpaCalc(`Zakaria`,50,65,38)
    student1.result() 

I am making a GPA calculator. Where the programme will take out percentage from 3 added subjects. if any students get less than 40% in average, it will show console.log(${this.firstName}, You have Failed!) and also if anyone gets less than 40 in a particular sub , it will show You have Failed as from 1st else if line. But after adding that else if (in first else if line) the result is showing `You have failed from first else if , though my Student1 gets above 40

Upvotes: 0

Views: 75

Answers (4)

vwadhwa3
vwadhwa3

Reputation: 163

According to my understanding of your problem, this could be a possible solution.

class GpaCalc{
    constructor(firstName,math,phy,chem){
        this.firstName = firstName;
        this.math = math;
        this.phy = phy;
        this.chem =chem;
    }

    result(){
        debugger
        let totalNum = this.math + this.phy + this.chem;
        let percentage = (totalNum * 100) / 300;
        if( this.firstName === ``){
            alert("Please Enter username.")

         
        } else {
            if (percentage < 40) {  
                alert(`${this.firstName}, You have Failed!`)
            }else
            {
            if ( this.math < 40 || this.phy < 40 || this.chem< 40)
                {
                alert("You have Failed!")
             }else{
                    if (percentage >= 80){
                        alert(`${this.firstName}, You got A+`)
                    } else if (percentage >= 70){
                        alert(`${this.firstName}, You got A`)
                    } else if (percentage >= 65){
                        alert(`${this.firstName}, You got A-`)
                    } else if (percentage >= 60){
                        alert(`${this.firstName}, You got B`)
                    } else if (percentage >= 50){
                        alert(`${this.firstName}, You got C`)
                    } else if (percentage >= 40){
                        alert(`${this.firstName}, You got D`)                                
                    }
                
                }                
            }
            
        }
      
    }}

    const student1 = new GpaCalc(`Zakaria`,50,65,38)
    const student2 = new GpaCalc(`Zakaria`,38,38,38)
    const student3 = new GpaCalc(`Zakaria`,60,60,60)
    student1.result();
    student2.result();
    student3.result();

Upvotes: 0

George Stavrou
George Stavrou

Reputation: 532

First of all your if else statement should be more clear. Also some mistakes in the if statements this.math > 40 || this.phy < 40 || this.chem < 40. You have this.math > 40.... and the other bellow... Thats why its not working....

My opinion

class GpaCalc {
    constructor(firstName, math, phy, chem) {
        this.firstName = firstName;
        this.math = math;
        this.phy = phy;
        this.chem = chem;
    }

    result() {
        let totalNum = this.math + this.phy + this.chem;
        let percentage = (totalNum * 100) / 300;
        if (this.firstName === ``) {
            console.log("Please Enter username.")
            //Problem is in this line below
        } else {
            if (this.math < 40 || this.phy < 40 || this.chem < 40) {
                console.log("You have Failed!")
            } else if (percentage >= 80) {
                console.log(`${this.firstName}, You got A+`)
            } else if (percentage >= 70) {
                console.log(`${this.firstName}, You got A`)
            } else if (percentage >= 65) {
                console.log(`${this.firstName}, You got A-`)
            } else if (percentage >= 60) {
                console.log(`${this.firstName}, You got B`)
            } else if (percentage >= 50) {
                console.log(`${this.firstName}, You got C`)
            } else if (percentage >= 40) {
                console.log(`${this.firstName}, You got D`)
            } else {
                console.log(`${this.firstName}, You have Failed!`)
            }
        }

    }
}



        var student1 = new GpaCalc(`Zakaria`, 50, 65, 42)
        student1.result() 

Upvotes: 1

Reality
Reality

Reputation: 677

Your logic in the this.math statement of the conditional seems to be mixed up.

It should be this.math < 40, not this.math > 40.

Sometimes a quick review of your code will suffice, instead of asking it on a forum that typically doesn't take kindly to extremely easily spotted mistakes.

In this case, you seem to have accidentally mixed the greater-than operator (>) with the less-than operator (<).

I would recommend in the future that you look over your code when something like this happens.

If your code has a lot of logic, usually it's something that has to do with the logic, not the language or language implementations.

If you're really stuck, then feel free to ask us a question, we'll do the best we can!

I would just recommend you look over your code a bit more before asking a question (I learned the hard way, and believe me - I don't want to do it again) and you will be fine :)

Upvotes: 0

Michael Santiago
Michael Santiago

Reputation: 1

It's going to fail not just because of this.math > 40 should be <40, but also because your chem score is 38, and you entered: "|| this.chem < 40".

Upvotes: 0

Related Questions