Jota D' Castro
Jota D' Castro

Reputation: 23

error for - if : The results are not as expected

I don't get the values that I should get - You can see here: https://stackblitz.com/edit/angular-nhvnwz

countStateStudents(){
  for (let student of this.students){
if (student.state == 1){
  this.signedup++;
} else
if (student.state == 2){
  this.boughtPIN++;
} else
if (student.state == 3){
  this.enrolled++;
} else
if (student.state == 4){
  this.notinterested++;
}
 }

Upvotes: 2

Views: 86

Answers (1)

Nikhil
Nikhil

Reputation: 6641

You are getting unexpected values because you are calling countStateStudents() multiple times. And the values are incremented every time you call the method.

Instead, you should just call the method once, and access the values returned without calling it again.

Component:

ngOnInit() {    

    const status = this.studentService.countStateStudents();

    this.signedup = status.incritos;
    this.boughtPIN = status.compropin;
    this.enrolled = status.matriculado;
    this.notinterested = status.nointeresado;
}

Upvotes: 2

Related Questions