how to generate random size array for sorting visualizer purpose using html css js?

Viewed 101

Array_picture

I am currently working on a project sorting visualizer and I need help for the array generation like in the given picture. please help me

1 Answers

This functions will generate for you an array of random length with random number (with the ranges you determine), you can use it in your visualizer.

function createRandomSizeArray(minArrayLength, maxArrayLength, minColumnHeight, maxColumnHeight){
   let ArrayLength = getRandomInt(minArrayLength, maxArrayLength);
   let arr = new Array(ArrayLength);
   for(let i = 0; i< arr.length; i++){
      arr[i] = getRandomInt(minColumnHeight, maxColumnHeight);
   }
   
   return arr;
}

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

console.log(createRandomSizeArray(5, 10, 2, 100));

Related