Ad
  • Custom User Avatar

    Ok, thanks, fixed. I was aware of the diffeence between one assertion and the other, I didn't know how you could write a wrong solution.

  • Custom User Avatar

    Example of wrong solution:

    export function stringToNumber(str: string): number {
      return  str as any as number
    
  • Custom User Avatar

    The error exist in TypeScript test.

    assert.equal use non-strict equality (==) and assert.equal(3, '3') does not throw error.

    assert.strictEqual use strict equality (===) and assert.strictEqual(3, '3') throw error.

    Current test:

    import { stringToNumber } from "./solution";
    import { assert } from "chai";
    
    describe("stringToNumber", function() {
      it("should work for the examples", function() {
        assert.equal(stringToNumber("1234"),1234);
        assert.equal(stringToNumber("605"), 605);
        assert.equal(stringToNumber("1405"),1405);
        assert.equal(stringToNumber("-7"),  -7);
      });
      it("should work for random strings", function() {
        var i, t;
        for(i = 0; i < 100; ++i){
          t = Math.round(Math.random() * 1e6 - 5e5);
          assert.equal(stringToNumber(t.toString(10)), t);
        }
      });
    });
    

    My porposal:

    import { stringToNumber } from "./solution";
    import { assert } from "chai";
    
    describe("stringToNumber", function() {
      it("should work for the examples", function() {
        assert.strictEqual(stringToNumber("1234"),1234);
        assert.strictEqual(stringToNumber("605"), 605);
        assert.strictEqual(stringToNumber("1405"),1405);
        assert.strictEqual(stringToNumber("-7"),  -7);
      });
      it("should work for random strings", function() {
        var i, t;
        for(i = 0; i < 100; ++i){
          t = Math.round(Math.random() * 1e6 - 5e5);
          assert.strictEqual(stringToNumber(t.toString(10)), t);
        }
      });
    });
    

    Doc assert.equal:
    https://www.chaijs.com/api/assert/#method_equal

    Doc assert.strictEqual
    https://www.chaijs.com/api/assert/#method_strictequal

    
    
  • Custom User Avatar

    Do you mind explaining why is that an issue? It seems to work fine.

  • Custom User Avatar

    In TypeScript tests use assert.strictEqual insted assert.equal