Changed the javascript coding style

Changed to the "sieve" style, where we first sieve out invalid inputs
and then assume that the input is correct.