Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
Why is it that I am getting 'Uncaught TypeError: Cannot read properties of undefined (reading 'sequence')'?
 const rndInt = Math.floor(Math.random() * 50);
 const arr = [];
 const input = document.getElementById('answer').value;

function displaySeq() {
        var i = 0;
        while (arr.length < 21) {
          arr[i] = {
              sequence: [],
              answer,
              guess: []
          };
          i++;

          for (j=0; j < 7; j++) {
              arr[i].sequence[0] = rndInt;
              arr[i].sequence.push(rndInt + j)
          }
       }  
        console.log(arr);
    }

I know that this error means that the variable does not have a value but I'm not sure what to do to resolve.

Another unrelated Question would be that my goal is that with every iteration a new random integer is the first element of each array. Right now it's the same random integer for each array. Is there a way to fix this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Observation : You are incrementing i after creating the array object and trying to access the array object at i index.

In simple way, you are trying to access arr[i+1] as at there is only one element in arr at this point of time.

Resolution : Increment the index i++ after completion of for loop.

Demo :

const rndInt = Math.floor(Math.random() * 50);
const arr = [];

function displaySeq() {
  var i = 0;
  while (arr.length < 21) {
    arr[i] = {
      sequence: [],
      answer: [],
      guess: []
    };

    for (j = 0; j < 7; j++) {
      arr[i].sequence[0] = rndInt;
      arr[i].sequence.push(rndInt + j)
    } 
    
    i++;
  }  
  console.log(arr);
}

displaySeq();

about 4 years ago · Juan Pablo Isaza Denunciar

0

You cannot access arr[0] when it is empty. So, first, you will have to push to the array and then call arr[0].

Also, you are incrementing i and then pushing into the array. So, initially, you are accessing arr[1] before defining arr[0].

Try this instead:

function displaySeq() {
     var i = 0;
     while (arr.length < 21) {
         arr.push({
             sequence: [],
             answer,
             guess: []
         });
         for (j=0; j < 7; j++) {
             arr[i].sequence[0] = rndInt;
             arr[i].sequence.push(rndInt + j)
         }
         i++;
     }  
     console.log(arr);
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda