I need to return in % the value of how many numbers are positive, the amount of zero and negative numbers, in this order, but my code is wrong

Asked

Viewed 72 times

0

function maisMenos(valores){
  var qtd = valores.length;
  var neg = valores.filter(nr=> nr < 0).length;
  var pos = valores.filter(nr=> nr > 0).length;
  var zer = valores.filter(nr=> nr ==0).length;
  var calcNeg = neg / qtd;
  var calcPos = pos / qtd;
  var calcZer = zer / qtd;
  var resultado = [];
  var insere = resultado.push(calcPos, calcZer, calcNeg);
  return resultado;
}
  • Although I can simplify some things, I think the only thing wrong is that we missed multiply the calcNeg, the calcPos and the calcZer for 100 each to be right.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

If the statement is correct, it is doing too much and the order of what it is verifying is wrong, as I think it is easy to observe this in the code.

function calculos(valores) {
    return [
      valores.filter(nr => nr < 0).length / valores.length * 100,
      valores.filter(nr => nr == 0).length / valores.length * 100,
      valores.filter(nr => nr > 0).length / valores.length * 100
    ];
}
console.log(calculos([-1, 0, 1, 0, 2, -2]));

I put in the Github for future reference.

If the statement is wrong the question is invalid.

Browser other questions tagged

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