Hi,
That version seems okay, except it parses the number twice (first for
isNaN, then again in parseFloat). There's also no need to check for a
blank string, '' can't be converted to a number and so parsing it will
result in NaN. Also, parseFloat defaults to base 10, so no need for
that param (but by all means include it if you think it makes the code
clearer).
var i;
var suma = 0;
var valor;
for (i = 1; i <= 24; i++) {
valor = parseFloat($F('quincena_'+ i).strip());
if (!isNaN(valor)) {
suma += valor;
}
}
Note that parseFloat will stop at the first invalid character, so this
doesn't do much in the way of validation. For instance, if the field
contains "15x5", parseFloat will return 15, not NaN. If you need real
validation, you'll probably want RegExps to test for valid patterns.
A web search should do it, if you need to take it that far.
FWIW,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available
On Jul 29, 12:56 am, "Miguel Beltran R." <[email protected]> wrote:
> Hi list
>
> I have the next code, but how can be made better?
>
> var i;
> var suma=0;
> var valor;
> for(i=1; i<=24; i++){
> valor=$F( 'quincena_'+ i).strip();
> if(valor!='' && !(isNaN(valor))){
> suma+=parseFloat(valor,10);
> }
> }
>
> --
> ________________________________________
> Lo bueno de vivir un dia mas
> es saber que nos queda un dia menos de vida
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---