Function with pEqual error

Asked

Viewed 260 times

0

Hello,

I am in the following statement trying to complete:

"Oh, but it doesn’t end here" - said Ana "I want to know how many months there was profit, IE, the balance was greater than zero" . Complete the quantityDeMesesComucro function. Example: Function quantityDeMesesComputer(a Precedent) { Let quantity= ??? ; for (Let mes=0; mes< umPeriodo.length; mes+) { ?? } quantity Return; }

And I made the following code:

function quantidadeDeMesesComLucro(umPeriodo){
let quantidade = 0;

for(let mes = 0; mes < umPeriodo.length; mes++){
    if(umPeriodo[mes] < 0)
        quantidade += 1;
}

return quantidade;

}

And the following errors appear stating that the solution has not completely passed:

x quantityDeMesesComucro([1]) is 1

0 deepEqual 1

x quantityDeMesesComucro([2, 20, 20]) is 3

0 deepEqual 3

x quantityDeMesesComucro([10, -10, 2, 100]) is 3

0 deepEqual 3

Can someone help me? Thank you!

  • Shouldn’t be umPeriodo[mes] > 0 instead of umPeriodo[mes] < 0?

  • The error remains in both ways.

1 answer

1


I do not think this question needs an answer, but as the user says that the code still does not work even with the correction in comparison, I will leave the snippet executable:

function quantidadeDeMesesComLucro(umPeriodo){
  let quantidade = 0;

  for (let mes = 0; mes < umPeriodo.length; mes++) {
    // compara se o valor do array umPeriodo na posição mês é maior do que 0
    if (umPeriodo[mes] > 0)
      quantidade += 1;
  }

  return quantidade;
}

console.log(quantidadeDeMesesComLucro([1]));
console.log(quantidadeDeMesesComLucro([2, 20, 20]));
console.log(quantidadeDeMesesComLucro([10, -10, 2, 100]));

As you can see, it printa 1, 3 and 3 respectively, the way it should.

  • user140828, thanks! This way solved on the platform!

Browser other questions tagged

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