Answer coming out only true for all elements, or false

Asked

Viewed 165 times

2

I was solving a question where there are two functions.

The first function is to know if the value is true or false with two parameters, if it is equal to or less than the second parameter will be true if not, false, returning it to the element.

The second function will use the first to use in a scaling way, e.g.: [true, false, true, true].

I put values for test end but it is not working very well at the time of printing.

If the qntMinima is value 0 or less all are true, regardless of the conditional, if it is greater than 0, all are false.

The printing of the function, there is a way for me to print only the element I want?

function trueoufalse(horario, qntMinima) {
    var contagem = 0;
    for (var a = 0; a < horario.length; a++) {
        if (horario[a] <= 0) {
            contagem = contagem + 1;
        }
    }
    if (contagem >= qntMinima) {
        return true;
    }
    if (contagem < qntMinima) {
        return false;
    }
}

function a(dias, qntMinima) {
    var qntMinima = 2;
    var dias = [2, -300, 40, -3];
    var conc = [];
    for (var a = 0; a < dias.length; a++) {
        conc.push(trueoufalse(dias[a], qntMinima));
    }
    return conc;
}
console.log(a()); //false,false,false,false

2 answers

3

The problem is you’re passing dias[a] for the function, which in this case will be a number. And within the function trueoufalse you try to pick up the attribute length of this number, but no length, and therefore the result of horario.length is undefined. That’s where it doesn’t come in for and the counter is always zero.

Actually you made a mess there. If the idea of function trueoufalse is to compare only two values, and return if one is greater than the other, then it should be just like this:

function maiorOuIgual(valor, qntMinima) {
    return valor >= qntMinima;
}

Just that, nothing else (I wonder if I really needed a function just for that, but as it seems to be an exercise that "requires" creating the function, so let’s leave it at that). Note that if the return is a boolean (true or false) i can directly return the result of the comparison, since every comparison returns a value boolean. In general, an expression like this:

if (condicao) {
    return true;
} else {
    return false;
}

Can be exchanged for:

return condicao;

I also changed the name of the function to something a little better, because in fact trueoufalse is the return, but what she actually does is to check whether the value passed is greater than or equal to the minimum amount reported. It may seem a minor detail, but giving better names helps a lot when programming.

Therefore, the function a could also rename. Another detail is that you declare parameters that are not used:

function a(dias, qntMinima) {
    var qntMinima = 2;
    var dias = [2, -300, 40, -3];
    ...
}

That is, you pass the variables dias and qntMinima, but in the first lines of the function already overwrites its values (that is, the parameters did not serve for nothing). Then either you declare the function without parameters:

function a() {
    var qntMinima = 2;
    var dias = [2, -300, 40, -3];
    ...
}

a();

Or you declare the parameters and pass the respective values in the function call:

function a(dias, qntMinima) {
    // não sobrescreve dias e qntMinima
    ...
}

// chama a função passando os parâmetros
a([2, -300, 40, -3], 2);

// chama a função passando outros parâmetros
a([1, -30, 933], 15);

The advantage of the second form is that the function is very generic and can receive any values (the first form will only work for the values you have placed).

The problem is that Javascript is very permissive and does not "error" when you pass a number of arguments other than what has been declared. So it’s up to us, who are programming, to pay attention to these details.


Therefore, a solution would be:

function maiorOuIgual(valor, qntMinima) {
    return valor >= qntMinima;
}

function verificarQuantidades(dias, qntMinima) {
    var conc = [];
    for (var a = 0; a < dias.length; a++) {
        conc.push(maiorOuIgual(dias[a], qntMinima));
    }
    return conc;
}

console.log(verificarQuantidades([2, -300, 40, -3], 2)); // [ true, false, true, false ]
console.log(verificarQuantidades([1, -30, 15, 2, 933], 15)); // [ false, false, true, false, true ]


Another alternative is to use the method map, which precisely serves to map each array value to another, returning another array with the results. So, you map each number to the respective comparison result:

function maiorOuIgual(valor, qntMinima) {
    return valor >= qntMinima;
}

function verificarQuantidades(dias, qntMinima) {
    return dias.map(n => maiorOuIgual(n, qntMinima));
}

console.log(verificarQuantidades([2, -300, 40, -3], 2)); // [ true, false, true, false ]
console.log(verificarQuantidades([1, -30, 15, 2, 933], 15)); // [ false, false, true, false, true ]

If the first function is not "mandatory", you can also do everything at once:

function verificarQuantidades(dias, qntMinima) {
    return dias.map(n => n >= qntMinima);
}

Or still, using for..of:

function verificarQuantidades(dias, qntMinima) {
    let result = [];
    for (const n of dias) result.push(n >= qntMinima);
    return result;
}

2

Its function trueoufalse receives only 1 hour as argument, therefore the for will never execute. Just make the comparison between the numbers, since the function a already iterates for the elements:

function trueoufalse (horario, qntMinima) {

  if (horario >= qntMinima) {
    return true;
  }
  if (contagem<qntMinima) {
    return false;
  }
}

function a (dias,qntMinima) { 
  var qntMinima = 2;
  var dias = [2,-300,40,-3];
  var conc = []; 
    for (var a=0;a<dias.length;a++) {
      conc.push(trueoufalse(dias[a], qntMinima));
    }
  
  return conc;
}
console.log(a()); // [true, false, true, false]

Another way to do it is by using the function map:

function a (dias,qntMinima) { 
  var qntMinima = 2;
  var dias = [2,-300,40,-3];
  var conc = []; 

  conc = dias.map((horario) => horario >= qntMinima)
  
  return conc;
}
console.log(a());

Browser other questions tagged

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