Ad
Strings
Parsing

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.

Code
Diff
  • 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;
    • }

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.

Code
Diff
  • 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`;
    • }
    • }