Hal Rosser said on 06/04/2006 2:53 PM AEST:
Ok, I read that FAQ 4.6
The code in that section is a marvelous display of someone's mastery of
javascript. I applaud the author.
The code, however, is missing comments (documentation) explaining what the
code is doing, and what the args represent. Someone looking for help (ie: a
beginner - or a programmer of lesser experience in javascript- like myself)
would likely come away from FAQ 4.2 with a sense of confusion. You have to
admit, someone else's undocumented code is sometimes difficult to follow.
Yes, completely, both in general and this particular case.
I also followed the first link listed in FAQ 4.2 to look for clearer
explanations on the art of rounding a number like 3.4589 to 2 decimals.
Then I followed the link (in FAQ 4.2) to a section of that page (General
Rounding section) and guess what!!! -
Look at the 3rd example in the "General Rounding" section of that link:
http://www.merlyn.demon.co.uk/js-round.htm#Gen
It sure looks like they multiplied by 100, then rounded it, then divided by
100 - doesn't it?
Yes, but the preceding text notes that[1]:
Math.round(1.035*100)/100 gives 1.03
but Math.round(2.035*100)/100 gives 2.04
Hopefully my formatting makes the error obvious.
This may seem trivial, however originally the difference between the two
numbers was 1.00, now it's 1.01. Depending on how that is used, a test
may pass or fail that should not have.
The circle is complete, and the wild goose we were sent to chasing suddenly
vanishes.
w3schools.com and devguru.com are not entirely without merit after all.
I do not like DevGuru at all, avoid it. I am less inclined to recommend
w3schools each time I visit. Both sites claim to be more than they are,
they are misleading their audience if they propose a flawed method of
rounding and do not point out the errors. What other errors are waiting
to be discovered? These sites are targeted at novices who know no
better, yet teach bad habits and fautly algorithms.
I understand criticism of JRS's coding style, IMHO it is unreasonably
concise, particularly when it is aimed at education. But for all his
faults, he is nearly always factually correct in regard to the topics
discussed at
www.merlyn.demon.co.uk/js*.
1. To JRS, if lurking, note the addition of '/100' in the quoted text.