How to declare IF in Ionic

Asked

Viewed 1,155 times

0

I am with the following code, it checks the value of _meditaTotal if it is greater than 70 the icone variable gets 'green', if it is lower q 70 and greater than 50 icone variable gets 'yellow' and if it is less than 50 icone variable gets 'red', but it is showing only red on the page.

public descritores;
public _mediaTotal: number;
public icone;

constructor(public navCtrl: NavController, public navParams: NavParams) {
this.descritores = navParams.get('anoSelecionado');
this._mediaTotal = this.descritores.icone;

    if (this._mediaTotal > 70){
      this.icone = 'verde';
    } 
    else if (this._mediaTotal < 70){
      this.icone = 'amarelo';
    }
    else (this._mediaTotal < 50)
      this.icone = 'vermelho';

}
  • this.descritores.icone is numerical type ?

  • Yes, this.descripores.icone are of the numeric type, with two decimal places, example: 81.16.

  • Check if this.descripores.icone is even coming as numeric using console.log(typeof this.descripores.icone); . To make sure you are comparing numeric by using in if ( Number(this._mediaTotal) > 70) etc.

2 answers

1

The problem is in the algorithm, for what you need you can use the following:

if (this._mediaTotal > 70){
  this.icone = 'verde';
}
else if (this._mediaTotal > 50){
  this.icone = 'amarelo';
}
else{
  this.icone = 'vermelho';
}

0

else if ((this._mediaTotal < 70) && (this._mediaTotal > 50)){
  this.icone = 'amarelo';
}

Fix the middle condition, in your case if average is 30 he and less than 70 and less than 50, check that it will only meet 1 condition.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.