-7

What does this code mean? I know that we are declaring var points as an array and we're using a loop but im not too experienced to fully understand. can someone elaborate?

  var points = [];
    for (var i = 0; i < x; i++) {
      points.push( random() );
    }
    return points;
  };
  • 5
    [Array.prototype.push()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push). It's the first match in a Google search for "javascript push". – Robert Harvey Oct 17 '19 at 00:37
  • I saw it but in my full code i do not use var points anywhere else and therefore i dont understand what its looping – Mathew Wizman Oct 17 '19 at 00:39

1 Answers1

-2

push is a function of array which adds a new object to the array of objects in the array

so this will call random x number of times and push it into the array so if x is 3 at the end you will have 3 random points if x was 7 there would be 7.

random() is another function call that returns a point I am guessing.

var points = [];  //Initializes an empty array
    for (var i = 0; i < x; i++) { // loops x number of times
      points.push( random() );  //Calls method random and adds return value to array
    }
    return points; // returns array with x number of points
  };
Derek Lawrence
  • 1,371
  • 1
  • 15
  • 31