Even or Odd without using a conditional statement (?)



  • I think the quality of my function speaks for itself in regards to how seriously I take the subject of this thread, and the amount of time I took working on it was directly proportional to that seriousness and/or quality.  Aside from that, however, I conceded your point about the loop technically constituting a conditional statement and amended my function accordingly in order to assuage any semantic misgivings.



  • Obligatory regular expression solution (with bonus javascript!):

    function even(x) {
        var results = { true : ! (x % 2), false : NaN };
    
    return results[ /^[+-]?[0-9]+$/.test(x) ];
    

    }
    var types = { true : "Even", false : "Odd", NaN : "Not an Integer" };
    var x = prompt("Type a number");

    alert(types[even(x)]);


Log in to reply