Javascript print arrays

<!DOCTYPE html>
<html>
<head>
    <title>100-Numbers</title>
</head>
<body>
    <script>
    function myarray()
      var points = [];
      for (var i = 0; i < 10000; i++) {
          points.push[Math.round(Math.random() * 10)];
          document.write(myarray(points));
      }
    </script>
    <<button onclick="myarray"> OK </button>>
</body>
</html>

I am a beginner with javascript. I want to create a page that displays random numbers from 0 – 10. I am struggling to print an array. Is there a simple solution to this?

Answer

  1. You should call the function from outside the function, not in the loop.
  2. The function can just return the array, you can print it in the caller.
  3. points.push is a function, you call it with (), not [].
  4. You’re missing {} around the function body.
  5. The function doesn’t take any arguments.

function myarray() {
  var points = [];
  for (var i = 0; i < 100; i++) {
    points.push(Math.round(Math.random() * 10));
  }
  return points;
}

console.log(myarray());