How to declare IF on Ionic

0

I have the following code, it checks the value of _meditaTotal if it is greater than 70 the variable icon receives 'green' if it is less than 70 and greater than 50 variable icon receives 'yellow' and if it is less than 50 variable icone gets 'red', but it is showing up just 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';

}
    
asked by anonymous 21.10.2017 / 21:57

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';
}
    
06.09.2018 / 16:56
0
else if ((this._mediaTotal < 70) && (this._mediaTotal > 50)){
  this.icone = 'amarelo';
}

Fix the condition of the medium, in your case if media is 30 and less than 70 and less than 50, make sure it only meets 1 condition.

    
28.12.2017 / 03:34