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;
}