Jump to content
Larry Ullman's Book Forums

Recommended Posts

For the Chapter 4 shopping example, on p.107: I've got it working when I enter valid numbers, just as in Figure 4.4, but when I enter the string 'cat' in the Quantity field, as you do in Figure 4.5, I get -3.00 in the Total field, not NaN as shown in the book. I realize that the eventual answer is going to be to validate all input before doing any calculations, but I'd still like to know what's going on here, and why my results are not agreeing with the book. Thanks.

Link to comment
Share on other sites

I got Firebug, and now I can see what was going on.

 

So this problem all stemmed from the use of the “number” type for the Quantity field, which is odd as it was pointed out on p.103, that it was done on purpose. I changed the type to “text” like all the other input fields, and the program behaved the way the book said it should: entering numbers in all the fields gave the correct numerical Total, while entering a string in any of the fields gave ‘NaN’ for the Total.

 

As an experiment I changed the type of all the fields to “number”. It still functions correctly when numbers are entered in all the fields, but gives an answer of ‘-3’ when  text is entered in Quantity. The worst possible outcome: an incorrectly calculated result with no indication that an error has occurred!

 

The strangest thing is the comment on p.103 that “…the number type doesn’t deal well with decimals.” Both the number type and the text type handled the decimal number computations just fine.

Link to comment
Share on other sites

 Share

×
×
  • Create New...