"Cannot read Property 'length' of Undefined" when using array arrays

Asked

Viewed 2,388 times

-1

A programming teacher, tired of students arriving late, decided to cancel class if there are few gifts.

It represents student input as an array of late arrival times in minutes. For example, if a student arrived 10 minutes late, another 5 minutes before the hour, another 3 minutes late, and another punctual, it may represent:

var alunosDaSegunda = [10, -5, 3, 0];

With this information and the minimum number of students to succeed the course, the teacher wants to know if the class will take place. For example, assuming that the minimum amount of students for the class to take place is 2 students, then the Monday course will take place, because there was a student who was punctual and a student who arrived early.

acontece(alunosDaSegunda, 2)
true

But if the minimum quantity was 3, the class would not happen:

acontece(alunosDaSegunda, 3)
false

Write the following functions: 1. happens, which says whether the class will succeed according to the array of incoming students. 2. openings, which uses an array with arrays of students who entered the other days, and the minimum amount of students, and tell which days the classes happened and which did not. For example:

aberturas([alunosDaSegunda, alunosDaTerça, alunosDaQuarta], 2)
[true, false, false]

I already solved the first part of the code, but this function 'openings()' that is giving error when I run. Follow the code I wrote.


var alunosDaSegunda = [10, -5, 3, 0, -2];
var alunosDaTerca = [5,-3,-8,-15,0];
var alunosDaQuarta = [0, -5, 4, 5, 10];

function acontece(chegada_alunos, min_alunos) {
  var alunos = 0;
  for (let i = 0; i <= chegada_alunos.length; i++) {
    if (chegada_alunos[i] <= 0) {
      alunos = alunos + 1;
    }
  }
  return alunos >= min_alunos;
}

function aberturas(dia_aulas, minimo_alunos) {
  var aulas_v = [];
  for (let a = 0; a <= dia_aulas.length; a++) {
    aulas_v.push(acontece(dia_aulas[a], minimo_alunos));
  }
  return aulas_v;
}

When I try to execute he presents me with this error:

aberturas([alunosDaSegunda, alunosDaTerca, alunosDaQuarta], 2)
solution.js:17
for (let i = 0; i <= chegada_alunos.length; i++) {
^
TypeError: Cannot read property 'length' of undefined
at acontece (solution.js:17:38)
at aberturas (solution.js:27:18)
at Object.<anonymous> (solution.js:34:31)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
  • 1

    for (let a = 0; a <= dia_aulas.length; a++), thus the value of a ranges from 0 to 3 inclusive, but in array you only have 0, 1 and 2. Your tie condition should be a < dia_aulas.length.

  • Taking advantage, in the next questions seek to elaborate a more descriptive title on the problem. " Exercise Classes" does not describe what you are doing and what the problem is to be solved in the question. Also note the consistency of the information, to avoid putting the error message different from the code you posted, as you did in this question, where the error is chegada_alunos.length and in the code dia_aulas.length. That doesn’t make sense.

  • It worked! Thank you so much! Thanks also for the tip...

1 answer

3


Hello! The reason for the above error is related to the fact that you are searching in a loop (for) the indicated days, but exceeding the array size.

for (let i = 0; i <= chegada_alunos.length; i++) {
    (...)
}

You are counting from position 0 to the limit of the array. You must use ( size -1 ) or just < in comparison.

This way the openings function, which has the same error is passing a non-existent array element which causes the error in the other function, making the case a little harder to find sometimes.

It is worth remembering that both are with the same problem in for.

Below is a possible correction to the error itself.

var alunosDaSegunda = [10, -5, 3, 0, -2];
var alunosDaTerca = [5,-3,-8,-15,0];
var alunosDaQuarta = [0, -5, 4, 5, 10];

console.log( acontece(alunosDaSegunda, 2));
console.log( aberturas([alunosDaSegunda, alunosDaTerca, alunosDaQuarta], 2));
 
function acontece(chegada_alunos, min_alunos) {
  var alunos = 0;
  for (let i = 0; i < chegada_alunos.length; i++) {
       if (chegada_alunos[i] <= 0) {
           alunos = alunos + 1;
      }
  }
  return alunos >= min_alunos;
}

function aberturas(dia_aulas, minimo_alunos) {
  var aulas_v = [];
  for (let a = 0; a < dia_aulas.length; a++) {
    aulas_v.push(acontece(dia_aulas[a], minimo_alunos));
  }
  return aulas_v;
}

Browser other questions tagged

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