[thelist] Why does this JS code work the way it does???

Matt Warden mwarden at gmail.com
Tue Dec 12 20:02:04 CST 2006

On 12/12/06, tim <tim at hyperlinkage.com> wrote:
> You've used four lines of code which could have simply been:
> return ! (strValidKeys.indexOf(String.fromCharCode(event.keyCode)) == -1)

... which saves you what, exactly? Certainly not smacks upside the
head. You've cut the lines of code down from 5 to 2. If I have any
complaints about the code Anthony brought for discussion, it's that
it's too short. I would want (formatting only -- ignoring the
programming choices):

function CheckNumericKeyStroke()
   var strValidKeys = "1234567890";

   // if there keyCode is not one of the numbers in validkeys...
   if (strValidKeys.indexOf(String.fromCharCode(event.keyCode)) == -1) {
       return false; // do not allow the keystroke
   else {
       return true; // allow the keystroke

Remember: every line you save for the sake of saving it almost
certainly costs your company money in maintenance costs. This is a
concept some programmers never seem to get and they instead focus on
filesize (which costs a company next to nothing) and execution speed
(which costs the company very little in 90% of the cases).

Matt Warden
Cleveland, OH, USA

This email proudly and graciously contributes to entropy.

More information about the thelist mailing list