Ad
Code
Diff
  • export type BmiProps = {
      height: number;
      weight: number;
    };
    
    export type CalculateBmiProps = BmiProps & {
      system: "metric" | "imperial";
    };
    
    export const imperialToMetric = ({ height, weight }: BmiProps): BmiProps => {
      return { height: height * 0.3048, weight: weight * 0.453592 };
    };
    
    export const calculateBmi = (
      { height, weight, system }: CalculateBmiProps,
    ): number => {
      const { height: h, weight: w } = system === "imperial"
        ? imperialToMetric({ height, weight })
        : { height, weight };
      
      return Math.round((w / (h ** 2)) * 100) / 100;
    };
    • export default class BMI {
    • private _height: number;
    • private _weight: number;
    • constructor(weight: number, height: number) {
    • this._height = height;
    • this._weight = weight;
    • }
    • set height(height: number) {
    • if (height <= 0) {
    • const invalidValueError = new Error('Invalid value');
    • console.error(invalidValueError);
    • }
    • this._height = height;
    • }
    • set width(weight: number) {
    • if (weight <= 0) {
    • const invalidValueError = new Error('Invalid value');
    • console.error(invalidValueError);
    • }
    • export type BmiProps = {
    • height: number;
    • weight: number;
    • };
    • this._weight = weight;
    • }
    • get bmi(): number {
    • return Number((this._weight / Math.pow(this._height, 2)).toFixed(2));
    • }
    • export type CalculateBmiProps = BmiProps & {
    • system: "metric" | "imperial";
    • };
    • export const imperialToMetric = ({ height, weight }: BmiProps): BmiProps => {
    • return { height: height * 0.3048, weight: weight * 0.453592 };
    • };
    • export const calculateBmi = (
    • { height, weight, system }: CalculateBmiProps,
    • ): number => {
    • const { height: h, weight: w } = system === "imperial"
    • ? imperialToMetric({ height, weight })
    • : { height, weight };
    • calculateBMI(): string {
    • return `there is ${this.bmi < 25 ? 'no ' : ''}excess weight`;
    • }
    • }
    • return Math.round((w / (h ** 2)) * 100) / 100;
    • };