I appreciate your need to vent, but --
@radarbob said:
The Superfluously Unnecessary Code bugExplicitly setting a variable to zero then immediately testing it for empty string? Priceless.
The code you posted actually doesn't quite do that.
The test relates to "num.value" with "num" being a Number object (created by "var num = 0") that, by default, doesn't have a "value" property, so "num.value" is guaranteed to be undefined in this context (but still a valid property lookup, even if it's pointless).
I'd venture the guess that the author really wanted to write "num
ber.value" (since the "number" parameter appears to actually relate to an HTML form element with a "value" property, not a scalar numeric value as suggested by the name) and was misled by his or her own confusing (lack of a) naming convention. "num" (a numeric value) in contrast to "number" (an HTML form element)? Sigh.
@radarbob said:
The "JavaScript Let's me be sloppy, so I will" bugA JS function signature does not include a return type. Note how this function returns a number sometimes or a string sometimes.
I don't see that.
The function you posted has exactly one "return" statement, and that statement always returns a number -- either the "0" value "num" was initialized with, the numeric result of the conversion performed by the function (the "num = str * 1" serves as a poor developer's typecast), or the pseudo-numeric "NaN" value if the input value does not happen to look like a valid number after stripping no more than three commas from it.
That said, here's my shot at a slightly better and considerably shorter replacement (note that it expects a scalar value instead of an HTML form element reference like the original code did):
[code]// Removes all commas from the stringified value of the argument
// and returns its numeric value, if possible, or NaN otherwise.
function RemoveCommas(number)
{
return Number(String(number).replace(/,/g, ""));
}[/code]