You have several problems in your code.
In the seAbre
function you have an argument with name alumnnos
instead of alumnos
: you have an "n" left over.
According to the statement and the examples of the tests, the function seAbre
does not receive an array of arrays, but a single array with the delays of one day. It is not necessary to make the two nested loops, you simply have to go through the received array seeing which elements are less than or equal to 0.
Likewise, the return value of this function should not be an array, but a Boolean value ( true
or false
) indicating if there were enough students.
In the first loop for
you take as ending value ingresos[i].length
, instead of ingresos.length
.
The second function aperturas
does return the expected values.
function seAbre(ingresos, alumnos) {
var alumnosTemprano = 0;
for (var i = 0; i < ingresos.length; i++) {
if (ingresos[i] <= 0) {
alumnosTemprano++
}
}
if (alumnosTemprano >= alumnos) {
return true;
} else {
return false;
}
return abre;
}
function aperturas(array, alumnos) {
var resultado = [];
for (var i = 0; i < array.length; i++) {
var alumnosLlegada = 0;
for (var j = 0; j < array[i].length; j++) {
if (array[i][j] <=0) {
alumnosLlegada++
}
}
if (alumnosLlegada >= alumnos) {
resultado.push(true)
} else {
resultado.push(false)
}
}
return resultado;
}
console.log(seAbre([10, -5, 3, 0], 4));
console.log(seAbre([10, -5, 3, 0], 3));
console.log(seAbre([10, -5, 3, 0], 2));
console.log(seAbre([10, -5, 3, 0], 1));
console.log(seAbre([0, 0, 3, 0], 3));
console.log(aperturas([[0,0,3,0],[1,2,4,5],[0,0,-1]],3));
console.log(aperturas([[0,0,-3,0],[1,2,4,5],[0,0,-1]],4));
This problem could also be solved by using the function seAbre
from aperturas
:
function seAbre(ingresos, alumnos) {
var alumnosTemprano = 0;
for (var i = 0; i < ingresos.length; i++) {
if (ingresos[i] <= 0) {
alumnosTemprano++
}
}
if (alumnosTemprano >= alumnos) {
return true;
} else {
return false;
}
return abre;
}
function aperturas(array, alumnos) {
var resultado = [];
for (var i = 0; i < array.length; i++) {
resultado.push(seAbre(array[i], alumnos));
}
return resultado;
}
console.log(seAbre([10, -5, 3, 0], 4));
console.log(seAbre([10, -5, 3, 0], 3));
console.log(seAbre([10, -5, 3, 0], 2));
console.log(seAbre([10, -5, 3, 0], 1));
console.log(seAbre([0, 0, 3, 0], 3));
console.log(aperturas([[0,0,3,0],[1,2,4,5],[0,0,-1]],3));
console.log(aperturas([[0,0,-3,0],[1,2,4,5],[0,0,-1]],4));
And a last example using the methods filter
and map
of object Array
:
var seAbre = (ingresos, alumnos) =>
(ingresos.filter(x => x<=0).length >= alumnos);
var aperturas = (ingresos, alumnos) =>
ingresos.map(i => seAbre(i, alumnos));
console.log(seAbre([10, -5, 3, 0], 4));
console.log(seAbre([10, -5, 3, 0], 3));
console.log(seAbre([10, -5, 3, 0], 2));
console.log(seAbre([10, -5, 3, 0], 1));
console.log(seAbre([0, 0, 3, 0], 3));
console.log(aperturas([[0,0,3,0],[1,2,4,5],[0,0,-1]],3));
console.log(aperturas([[0,0,-3,0],[1,2,4,5],[0,0,-1]],4));