Add elements from an array

0

I have the problem that they ask me to do a function cantidadDeMesesConGanancias that I add the positive elements ... I do ...

Function cantidadDeMesesConGanancias(meses){
Var sumatoria=0
For(Var i=0;i <meses.length;i++){
If(meses[i] ›0){
Sumatoria=sumatoria+meses
}
}
Return sumatoria}

And he gives me an error

    
asked by Edy Lacity 29.09.2018 в 03:47
source

3 answers

4

There are many errors in your code.

  • Reserved words function , var , for , if and return they must go completely in lowercase.
  • On line 5 of your code, the variable summation must be in lowercase.
  • Also on line 5, you are adding the variable summation and months when you should add summation and 1.

In the end, your code should look like this: (already correctly embedded)

function cantidadDeMesesConGanancias(meses) {
    var sumatoria = 0;
    for ( var i = 0; i < meses.length; i++ ) {
        if ( meses[i] > 0 ) {
            sumatoria = sumatoria + 1;
        }
    }
    return sumatoria;
}
    
answered by 29.09.2018 в 05:50
0

In whatever programming language you have to try to use the notation camelCase in the definition of variables and functions. On the other hand Javascript as every language has a set of reserved words MDN Javascript reserved words

at the end the code should stay like this:

function cantidadDeMesesConGanancias(meses) {
var sumatoria = 0;
for ( var i = 0; i < meses.length; i++ ) {
    if ( meses[i] > 0 ) {
        sumatoria++;
    }
}
return sumatoria; }
    
answered by 29.09.2018 в 16:20
-1

Because you do this:

Sumatoria=sumatoria+meses;

It should be:

sumatoria=sumatoria+1;
    
answered by 29.09.2018 в 04:32