1

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;
  }
}
     
3
  • 1
    As long as this.productWorth is undefined, all conditions will evaluate to false. Commented Sep 6, 2022 at 10:18
  • Please give a minimal reproducible example; in what you're showing productWorth never changes from its initial value of undefined, so it's unclear why you'd expect any other condition to match. Commented Sep 6, 2022 at 10:18
  • Here is a link to the code on plnkr link Commented Sep 6, 2022 at 12:13

2 Answers 2

1

Your code doesn't show any kind of change of the productWorth field. Hence we can just assume, that it keeps being undefined, which will always result into 0.

Please don't mind me giving you a recommendation along. Remove this horror if-else construct. Instead you can do this:

// must be sorted
var myPriceTable = {
  200: 150,
  2001: 450,
  5001: 900,
  10001: 1200,
  20001: 1500
}

function changepricePerProduct(productWorth) {
  if (typeof productWorth !== 'number') return 0;
  
  var floorKey = Object.keys(myPriceTable).findLast(key => key < productWorth);

  return floorKey ? myPriceTable[floorKey] : 0;
}

console.log(changepricePerProduct(undefined));
console.log(changepricePerProduct(5));
console.log(changepricePerProduct(222));
console.log(changepricePerProduct(5555));
console.log(changepricePerProduct(22222));

Sign up to request clarification or add additional context in comments.

3 Comments

findLast is returning an error, please help Property 'findLast' does not exist on type 'string[]'.ts(2339)
Array.prototype.findLast is pretty new. In older ES-versions it is not available for you. However, you can instead use lodash's findLast for example.
You can also declare your object in inverse mode and use find (or use an array of object with properties: key and value. like this "hard copy of Moxxi idea's" stackblitz
0
changepricePerProduct() {
 return this.productWorth >= 200 && (this.productWorth) <= 2000 ? 150 : 
 (this.productWorth >= 2001 && (this.productWorth) <= 5000)?450: 
 (this.productWorth >= 5001 && (this.productWorth) <= 10000)?900: 
 (this.productWorth >= 10001 && (this.productWorth) <= 20000)?1200: 
 (this.productWorth >= 20000)?1500:0 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.