Quantifying negative values in an array

Asked

Viewed 1,784 times

-2

I’m trying to do an exercise, but it was not the way the platform wanted, I’m without ideas on how to solve, I’m on my first day of javascript.... Follow the statement, the error and my attempt.

A company sent a list containing the monthly numbers of everything she billed, and our job is to help them create a report that shows how many months they had the negative balance.

var listaDeGanhos = [10, 30, -10, -5, -1, 40]

Based on the array above, which is available in the code, loop check how many months have had negative values and store the count a variable called totalNegative which is also available in the code.


In the Code it’s already written

var listaDeGanhos = [10, 30, -10, -5, -1, 40]

var totalNegativos = 0


//minha tentativa 

for ( var i = 0;  i < listaDeGanhos.length; i++){
    numeros = listaDeGanhos[i]
    if ( numeros < 0 ){
        console.log(totalNegativos++) 
    }

}

Error I get : You should check if the current value of the array is negative with an if

  • Would it be possible to be more clear about the problem? Your code is working correctly. If the problem is in relation to printing, just put the operator ++ before the variable. This way the increment will happen before printing the value.

  • Hard to say what’s wrong, because it looks like the problem is in the algorithm that’s analyzing the logic of your code. I would deduce that he is failing to interpret this indirect that you created with the variable numeros, he must be hoping that in if you use if (listaDeGanhos[i] < 0), but that’s just a hunch, because there’s no way to replicate your problem.

  • @user140828 was exactly that, the algorithm of the platform I am using did not recognize the variable numbers. I did as you indicated and it worked, thank you.

6 answers

2

There are several possible solutions, if the statement asks only the amount of negative numbers, the solution below is simple and does what is asked, without displaying a console with the number that is negative.

var listaDeGanhos = [10, 30, -10, -5, -1, 40]
var totalNegativos = 0

//seu loop aqui:
for(var i = 0; i < listaDeGanhos.length; i++){ 
    if(listaDeGanhos[i] < 0)
     totalNegativos++
} 
console.log(totalNegativos);

now the most beautiful solution would be this, but that perhaps the code parser does not accept:

var listaDeGanhos = [10, 30, -10, -5, -1, 40];

const negativos = listaDeGanhos.filter(number => number < 0 );

console.log(negativos.length);

1

let listaDeGanhos = [10, 30, -10, -5, -1, 40]

let totalNegativos = 0

listaDeGanhos.forEach(function (valor) {

    if(valor < 0){
        totalNegativos++
    }
})

console.log(totalNegativos) // 3

Now if the platform really demands this way

var listaDeGanhos = [10, 30, -10, -5, -1, 40]

var totalNegativos = 0

for ( var i = 0;  i < listaDeGanhos.length; i++){

    if ( listaDeGanhos[i] < 0 ){
        totalNegativos++ // Acrescentando no total
        console.log(listaDeGanhos[i]) // Mostrando quanto foi
    }
}

// Mostre após o loop
console.log(totalNegativos)

-1

var listaDeGanhos = [10, 30, -10, -5, -1, 40]
var totalNegativos = 0

for (var i = 0; i < listaDeGanhos.length; i++){
  if(listaDeGanhos[i] < 0){
    totalNegativos ++; 
  }
 
}
console.log(totalNegativos)

-1

for me, it worked like this:

var listaDeGanhos = [10, 30, -10, -5, -1, 40] var totalNegatives = 0

for (i = 0; i < list++) {

console.log // here I just ran the count without adding to validate the array if (listDeGanhos[i] < 0) { console.log(totalNegatives++) //o if validates negatives and sum

}

}

-2

var listaDeGanhos = [10, 30, -10, -5, -1, 40]
var totalNegativos = 0
for ( var i = 0;  i < listaDeGanhos.length; i++){
        if ( listaDeGanhos[i] < 0 )
        totalNegativos++
    }
  • I’m doing the same exercise, should be on the same platform, and the code that accepted was this, if you put console.log... will give error

-2


Pass there as your code please! I am trying to follow your structure modifying here but it is not returning correctly. Follow the code:

var listaDeGanhos = [10, 30, -10, -5, -1, 40]

var totalNegatives = 0

//your loop here: for(var i = 0; i < listDeGanhos.length; i++){ totalNegatives = listGanhos[i]
if(listGanhos[i] < 0){ console.log(totalNegative++) }

}

var listaDeGanhos = [10, 30, -10, -5, -1, 40]
var totalNegativos = 0

//seu loop aqui:
for(var i = 0; i < listaDeGanhos.length; i++){ 
    totalNegativos = listaDeGanhos[i]  
if(listaDeGanhos[i] < 0){
   console.log(totalNegativos++)
}

} 

  • I appreciate the help, but I’ve managed to accomplish the way the platform demanded, and it was exactly as you suggested

  • There is an error in this statement here: totalNegativos = listaDeGanhos[i], it is changing a value that should not change at this time. Only when the value is negative

Browser other questions tagged

You are not signed in. Login or sign up in order to post.