Ad

cleaned up a bit of the code

Code
Diff
  • export default class BMI {
      constructor(private _weight: number, private _height: number) {}
      
      set height(height: number) {
        if (height <= 0) {
          console.error(new Error('Invalid value'));
          return;
        }
        this._height = height;
      }
      
      set weight(weight: number) {
        if (weight <= 0) {
          console.error(new Error('Invalid value'));
          return;
        }
    
        this._weight = weight;
      }
      
      get bmi(): number {
        return round((this._weight / this._height ** 2), 2);
      }
      
      calculateBMI(): string {
        return `there is ${this.bmi < 25 ? 'no ' : ''}excess weight`;
      }
    }
    
    const round = (num: number, digits: number) => Math.round(num * 10 ** digits) / 10 ** digits;
    • export default class BMI {
    • private _height: number;
    • private _weight: number;
    • constructor(weight: number, height: number) {
    • this._height = height;
    • this._weight = weight;
    • }
    • constructor(private _weight: number, private _height: number) {}
    • set height(height: number) {
    • if (height <= 0) {
    • const invalidValueError = new Error('Invalid value');
    • console.error(invalidValueError);
    • console.error(new Error('Invalid value'));
    • return;
    • }
    • this._height = height;
    • }
    • set width(weight: number) {
    • set weight(weight: number) {
    • if (weight <= 0) {
    • const invalidValueError = new Error('Invalid value');
    • console.error(invalidValueError);
    • console.error(new Error('Invalid value'));
    • return;
    • }
    • this._weight = weight;
    • }
    • get bmi(): number {
    • return Number((this._weight / Math.pow(this._height, 2)).toFixed(2));
    • return round((this._weight / this._height ** 2), 2);
    • }
    • calculateBMI(): string {
    • return `there is ${this.bmi < 25 ? 'no ' : ''}excess weight`;
    • }
    • }
    • }
    • const round = (num: number, digits: number) => Math.round(num * 10 ** digits) / 10 ** digits;