Error showing cycle for

0

Why do not you show me anything?

window.addEventListener("load", function() {
  
  var matriz = new Array(50);
  matriz.forEach(function(i) {
     document.getElementById("forEach").innerHTML += i + '\t';
    
  });
  
});
body {  
  background-color: #E8A623;
}
h1,h2,h3,h4,h5,h6 {
  color: white;
 }
<!--<!DOCTYPE html>--->
<html>
  <head>
    <title>Bucles</title>
  </head>
  <body>
    <h1 style="text-align: center;">Bucle for simple |  1 - 10 |</h1>
    <hr></hr>
  <i style="text-align: center;"><i id="forEach"></i></i>
  </body>
</html>
    
asked by Eduardo Sebastian 12.06.2017 в 19:57
source

3 answers

2
The forEach() does not execute the function for array elements without values.

As the companion above mentioned you.

Your array is filled with values undefined , which are not considered values.

    
answered by 12.06.2017 / 20:12
source
0

Because your array is only full of Null, there's nothing to show ...

  var matriz1 = new Array(50);
console.log('tu matriz');
console.log(matriz1);
var matriz = [1,2,3,4]
console.log('matriz llena');
console.log(matriz);
  matriz.forEach(function(i) {
     document.getElementById("forEach").innerHTML += i + '\t';
  });
body {  
  background-color: #E8A623;
}
h1,h2,h3,h4,h5,h6 {
  color: white;
 }
<h1 style="text-align: center;">Bucle for simple |  1 - 10 |</h1>

  <i style="text-align: center;"><i id="forEach"></i></i>
    
answered by 12.06.2017 в 20:09
0

Your array is empty, it must be filled before being traversed, otherwise you will get an error.

$(document).ready(function(){
var matriz = [1,2,3,4,5,6,7,8,9,10]
  matriz.forEach(function(i) {
     document.getElementById("forEach").innerHTML += i + '\t';
  });
});
body {  
  background-color: #E8A623;
}
h1,h2,h3,h4,h5,h6 {
  color: white;
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<html>
  <head>
    <title>Bucles</title>
  </head>
  <body>
    <h1 style="text-align: center;">Bucle for simple |  1 - 10 |</h1>
    <hr></hr>
  <div style="text-align: center;" id="forEach"></div>
  </body>
</html>
    
answered by 12.06.2017 в 21:10