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;
- };
import { assert } from "chai"; import { calculateBmi } from "./solution"; describe('BMI Calculator', function() { it('BMI calculates correctly', function() { assert.equal(calculateBmi({ height: 6, weight: 160, system:'imperial' }), 21.7); assert.equal(calculateBmi({ height: 3, weight: 160, system:'metric' }), 17.78); }); });
// See https://www.chaijs.com for how to use Chai.- import { assert } from "chai";
import BMI from "./solution";const BMI_OK = "there is no excess weight";const BMI_NOT_OK = "there is excess weight";- import { calculateBmi } from "./solution";
- describe('BMI Calculator', function() {
it('BMI is ok testing', function() {assert.strictEqual(new BMI(55, 1.60).calculateBMI(), BMI_OK);assert.strictEqual(new BMI(50, 1.75).calculateBMI(), BMI_OK);assert.strictEqual(new BMI(77, 1.76).calculateBMI(), BMI_OK);});it('BMI is not ok testing', function() {assert.strictEqual(new BMI(75, 1.70).calculateBMI(), BMI_NOT_OK);assert.strictEqual(new BMI(100, 1.70).calculateBMI(), BMI_NOT_OK);- it('BMI calculates correctly', function() {
- assert.equal(calculateBmi({ height: 6, weight: 160, system:'imperial' }), 21.7);
- assert.equal(calculateBmi({ height: 3, weight: 160, system:'metric' }), 17.78);
- });
- });