cleaned up a bit of the code
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;
// 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"; const bmiTest = (message: string) => (bmi: BMI) => assert.strictEqual(bmi.calculateBMI(), message); describe('BMI Calculator', () => { it('incorrect weight', () => { const bmi = new BMI(4, 2); bmi.weight = -2; assert.strictEqual(bmi.bmi, 1); }); it('incorrect height', () => { const bmi = new BMI(4, 2); bmi.height = -2; assert.strictEqual(bmi.bmi, 1); }); it('BMI is ok', () => { const bmi_ok = bmiTest(BMI_OK); bmi_ok(new BMI(55, 1.60)) bmi_ok(new BMI(50, 1.75)); bmi_ok(new BMI(77, 1.76)); }); it('BMI is not ok', () => { const bmi_not_ok = bmiTest(BMI_NOT_OK); bmi_not_ok(new BMI(75, 1.70)); bmi_not_ok(new BMI(100, 1.70)); }); });
- // 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";
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);- const bmiTest = (message: string) => (bmi: BMI) => assert.strictEqual(bmi.calculateBMI(), message);
- describe('BMI Calculator', () => {
- it('incorrect weight', () => {
- const bmi = new BMI(4, 2);
- bmi.weight = -2;
- assert.strictEqual(bmi.bmi, 1);
- });
- it('incorrect height', () => {
- const bmi = new BMI(4, 2);
- bmi.height = -2;
- assert.strictEqual(bmi.bmi, 1);
- });
- it('BMI is ok', () => {
- const bmi_ok = bmiTest(BMI_OK);
- bmi_ok(new BMI(55, 1.60))
- bmi_ok(new BMI(50, 1.75));
- bmi_ok(new BMI(77, 1.76));
- });
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 is not ok', () => {
- const bmi_not_ok = bmiTest(BMI_NOT_OK);
- bmi_not_ok(new BMI(75, 1.70));
- bmi_not_ok(new BMI(100, 1.70));
- });
- });