2

I am creating an array by using the function below to add the numbers based on the size of my array. Running this creates the array in this order "5, 4, 3 ,2, 1"

I am trying to create this in random order instead of the order from the loop. Is there a way for me to do this?

function ArrayList() {
  let array = [];
  ArrayList.prototype.insert = function(item) {
    array.push(item);
  };
  ArrayList.prototype.toString = function() {
    return array.join();
  };
}

function createNonSortedRandomArray(size) {
  var array = new ArrayList();
  for (var i = size; i > 0; i--) {
    array.insert(i);
  }
  return array;
}

var array = createNonSortedRandomArray(5);
console.log(array.toString());

0 Answers0