Array js giving error [closed]

0

I have the following javaScript :

  var imagem = [];

  for (i = 0; i < quantasImagens; i++) {    

      tMin = t + tempoTransicao;
      tMax = t + tamanhoIntervalos; 
      t+=tamanhoIntervalos;

      if(i==0) tMin=0;
      if(i==quantasImagens) tMax=100;         


      imagem[i][0] = tMin + "% { margin-left:-" + tempoImagens + "%};";
      imagem[i][1] = tMax + "% { margin-left:-" + tempoImagens + "%};";

      tempoImagens+=100;

  }

  for (po=0; po<imagem.length; i++) {
      document.write(imagem[i][0]);
      document.write("<br />");
      document.write(imagem[i][1]);
      document.write("<br />");
      document.write("<br />");
  }

When the for does the second cycle, it gives error in the line:

      imagem[i][0] = tMin + "% { margin-left:-" + tempoImagens + "%};";

Saying that index 0 does not exist.

Where am I going wrong?

    
asked by anonymous 25.10.2017 / 20:52

1 answer

2

If each array array item is also an array, you need to declare them inside your loop:

imagem[i] = [];
imagem[i].push(tMin + "% { margin-left:-" + tempoImagens + "%};");
imagem[i].push(tMax + "% { margin-left:-" + tempoImagens + "%};");

Notice that I'm using push instead of directly assigning to each index. It works also the way you did, but I prefer push because it's the default way of inserting items into arrays.

    
25.10.2017 / 21:33