How do I write a if and if-else statement in Angular project? It's only the "else" that is returning, even though the initial conditions are true.
export class HomeComponent implements OnInit {
productWorth: number;
unit: number;
pricePerProduct:any = this.changepricePerProduct();
result: number;
constructor() {}
ngOnInit() {}
changepricePerProduct() {
if (this.productWorth >= 200 && (this.productWorth) <= 2000) {
return 150;
} else if (this.productWorth >= 2001 && (this.productWorth) <= 5000) {
return 450
} else if (this.productWorth >= 5001 && (this.productWorth) <= 10000) {
return 900
} else if (this.productWorth >= 10001 && (this.productWorth) <= 20000) {
return 1200
} else if (this.productWorth >= 20000) {
return 1500
} else {
return 0
}
}
multiply() {
this.result = this.pricePerProduct * this.unit;
}
}
this.productWorthisundefined, all conditions will evaluate tofalse.productWorthnever changes from its initial value ofundefined, so it's unclear why you'd expect any other condition to match.