Strings
Parsing
function stripEnds(s, prefix, suffix) { return s.replace(new RegExp(`[${prefix}${suffix}]`, 'gi'), ''); }
- function stripEnds(s, prefix, suffix) {
return s.replace(prefix, '').replace(suffix, '');- return s.replace(new RegExp(`[${prefix}${suffix}]`, 'gi'), '');
- }
const chai = require("chai"); const assert = chai.assert; describe("Solution", function() { it("Test Case", function() { assert.strictEqual(stripEnds("$1.00/day", "$", "/day"), "1.00"); }); });
// 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");
- });
- });
Fork from Python BMI calculator made via typescript.
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); } 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`; } }
from dataclasses import dataclass- 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);
- }
@dataclassclass BMI:height: floatweight: float@propertydef bmi(self) -> float:return self.weight / (self.height ** 2)def calculate_bmi(self) -> str:return f"there {'is no' if self.bmi < 25 else 'is'} excess 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 { 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); }); 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); }); });
import unittestfrom solution import BMI- // See https://www.chaijs.com for how to use Chai.
- import { assert } from "chai";
- import BMI from "./solution";
class TestBMICalculator(unittest.TestCase):def setUp(self) -> None:self.samples = [(75, 1.70, "there is excess weight"),(55, 1.60, "there is no excess weight"),(50, 1.75, "there is no excess weight"),(100, 1.70, "there is excess weight"),(77, 1.76, "there is no excess weight"),]- const BMI_OK = "there is no excess weight";
- const BMI_NOT_OK = "there is excess weight";
def test_bmi_results(self):for h, w, e in self.samples:self.assertEqual(BMI(w, h).calculate_bmi(), e)if __name__ == '__main__':unittest.main()- 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);
- });
- });