The object is to strip the prefix and suffix so the replace should only work if the prefix is at the start of the string and the suffix is at the end.
Expanding the test coverage indicates that we do not have working code.
function stripEnds(s, prefix, suffix) { let r = s; // Only remove prefix if it is present at start of string. if (r.slice(0, prefix.length) === prefix) { r = r.slice(prefix.length); } // Only remove suffix if it is present at end of string. if (r.slice(suffix.length * -1) === suffix) { r = r.slice(0, suffix.length * -1); } return r; }
- function stripEnds(s, prefix, suffix) {
return s.replace(prefix, '').replace(suffix, '');- let r = s;
- // Only remove prefix if it is present at start of string.
- if (r.slice(0, prefix.length) === prefix) {
- r = r.slice(prefix.length);
- }
- // Only remove suffix if it is present at end of string.
- if (r.slice(suffix.length * -1) === suffix) {
- r = r.slice(0, suffix.length * -1);
- }
- return r;
- }
const chai = require("chai"); const assert = chai.assert; describe("Solution", function() { it("Test Case", function() { assert.strictEqual(stripEnds("$1.00/day", "$", "/day"), "1.00"); assert.strictEqual(stripEnds("approx $1.00/day", "$", "/day"), "approx $1.00"); assert.strictEqual(stripEnds("$1.00/day approx", "$", "/day"), "1.00/day approx"); }); });
// Since Node 10, we're using Mocha.// You can use `chai` for assertions.- const chai = require("chai");
- const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:// chai.config.truncateThreshold = 0;// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.// Uncomment the following to use the old assertions:// const Test = require("@codewars/test-compat");- describe("Solution", function() {
- it("Test Case", function() {
// Test.assertEquals(1 + 1, 2);// assert.strictEqual(1 + 1, 2);- assert.strictEqual(stripEnds("$1.00/day", "$", "/day"), "1.00");
- assert.strictEqual(stripEnds("approx $1.00/day", "$", "/day"), "approx $1.00");
- assert.strictEqual(stripEnds("$1.00/day approx", "$", "/day"), "1.00/day approx");
- });
- });
The amends I've made ensure that the setter methods are used in the constructor. They weren't used at all and the name of the "set weight" method was "set width".
Here rather than writing the errors to console and continuing with the code we throw an error.
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) { throw new Error('Invalid value for height'); } this._height = height; } set weight(weight: number) { if (weight <= 0) { throw new Error('Invalid value for weight'); } this._weight = weight; } get bmi(): number { return Number((this._weight / Math.pow(this._height, 2)).toFixed(2)); } calculateBMI(): string { return `there is ${this.bmi < 25 ? 'no ' : ''}excess weight`; } }
- export default class BMI {
private _height: number;private _weight: number;- private _height!: number;
- private _weight!: number;
- constructor(weight: number, height: number) {
this._height = height;this._weight = weight;- this.height = height;
- this.weight = weight;
- }
- set height(height: number) {
- if (height <= 0) {
const invalidValueError = new Error('Invalid value');console.error(invalidValueError);- throw new Error('Invalid value for height');
- }
- this._height = height;
- }
set width(weight: number) {- set weight(weight: number) {
- if (weight <= 0) {
const invalidValueError = new Error('Invalid value');console.error(invalidValueError);- throw new Error('Invalid value for weight');
- }
- this._weight = weight;
- }
- get bmi(): number {
- return Number((this._weight / Math.pow(this._height, 2)).toFixed(2));
- }
- calculateBMI(): string {
- return `there is ${this.bmi < 25 ? 'no ' : ''}excess weight`;
- }
- }
// See https://www.chaijs.com for how to use Chai. import { expect, 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('verifies valid input', function() { expect(() => new BMI(1, 1)).to.not.throw(); expect(() => new BMI(0, 1)).to.throw('Invalid value for weight'); expect(() => new BMI(1, 0)).to.throw('Invalid value for height'); }); 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); }); });
- // See https://www.chaijs.com for how to use Chai.
import { assert } from "chai";- import { expect, 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('verifies valid input', function() {
- expect(() => new BMI(1, 1)).to.not.throw();
- expect(() => new BMI(0, 1)).to.throw('Invalid value for weight');
- expect(() => new BMI(1, 0)).to.throw('Invalid value for height');
- });
- 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);
- });
- });