vendredi 2 avril 2021

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

Aucun commentaire:

Enregistrer un commentaire