-3

HERE IS THE SUMMARY:

I prompt 3 value numbers for the array, if the addition total of the arrays elements are more than 0, i need to display "el funcionamiento no es infinito" otherwise "es infinito"

its not working due i think i am not calculating all element in the array danio_Total

HERE IS THE CODE, THANKS!


var comprobarTiempoFuncionamiento = function(i) {

    if (danio_Total.value > 0) {
        document.write("el funcionamiento no es infinito");
    } else {document.write("El tiempo de funcionamiento es infinito");}

}
Walid Ammar
  • 4,038
  • 3
  • 25
  • 48
DanArg
  • 37
  • 1
  • 6

3 Answers3

1

This is easier than it sounds, if you have an array and all you want to do is check if the sum is larger than zero, than you can reverse it and say that the sum would only be zero if all values in the array is zero (unless you have negative numbers), so this

[0,0,0,0]

would be the only type of array that would sum up to zero.

As zero's are falsy, you can just do

if ( danio_Total.filter(Boolean).length ) {
    document.write("el funcionamiento no es infinito");
} else {
    document.write("El tiempo de funcionamiento es infinito");
}

When filtering on Boolean, any positive integer will be truthy, so

[0,0,9].filter(Boolean).length; // returns 1, truthy
[0,3,9].filter(Boolean).length; // returns 2, truthy
[0,0,0].filter(Boolean).length; // returns 0, falsy

so it alls you really need.

adeneo
  • 312,895
  • 29
  • 395
  • 388
0

if you want know the length of array try this:

   for (i = 0; i < array.length; i++){
    sum += array[i]; 
   }
0

Try it like bellow

 var total = danio_toal.reduce(function(a,b){
                 return a+b;
             });

 if (total > 0) {
    document.write("el funcionamiento no es infinito");
} else {document.write("El tiempo de funcionamiento es infinito");}
Mritunjay
  • 25,338
  • 7
  • 55
  • 68