Move History

Fork Selected
  • Description

    This places the undefined cases together on the left side of the initial evaluation enhancing readability, and then executes the primary test for 'eveness' on the right side of the comparison, again, enhancing readability.

    Code
    isEven = x => !x || !Number.isInteger(x) ? undefined : x % 2 ? false : true;
    
    // Previous iteration
    // isEven = x => x!=~~x?undefined:!(x%2);
    
    // Previous iteration
    // isEven = num => num % 2 === 0 ? true : num % 1 === 0 ? false : undefined;
    
    // Previous iteration
    
    // function isEven(num) {
    //   return num % 2 === 0 ? true : num % 1 === 0 ? false : undefined;
    // }
    
    Preloaded Code
                                                                                                                                                              _=n=>!(n%1)
    
    Test Cases
    const expect = require("chai").expect;
    describe("Solution", function() {
      it("should be true for 4", function() {
        expect(isEven(4)).to.equal(true);
      });
      it("should be false for 1", function() {
        expect(isEven(1)).to.equal(false);
      });
      it("should be undefined for 1.5", function() {
        expect(isEven(1.5)).to.equal(undefined);
      });
      it("should be undefined for NaN", function() {
        expect(isEven(NaN)).to.equal(undefined);
      });
      it("should be undefined for Infinity", function() {
        expect(isEven(Infinity)).to.equal(undefined);
      });
      it("should be undefined for -Infinity", function() {
        expect(isEven(NaN)).to.equal(undefined);
      });
      it("should be undefined for undefined", function() {
        expect(isEven(undefined)).to.equal(undefined);
      });
    });
    
  • Code
    • isEven = x => x!=~~x?undefined:!(x%2);
    • isEven = x => !x || !Number.isInteger(x) ? undefined : x % 2 ? false : true;
    • // Previous iteration
    • // isEven = x => x!=~~x?undefined:!(x%2);
    • // Previous iteration
    • // isEven = num => num % 2 === 0 ? true : num % 1 === 0 ? false : undefined;
    • // Previous iteration
    • // function isEven(num) {
    • // return num % 2 === 0 ? true : num % 1 === 0 ? false : undefined;
    • // }