0 points

javascript - How to generate a random number from an array - Stack Overflow


dashboard

Fri Jun 19 2020 17:30:52 GMT+0000 (UTC)

Posted by @arkadiy #javascript

function bookRentData(bookData) {

   bookData.forEach((book) => {

      //generate random sku number

      const sku = book.sku
      var random = sku[Math.floor(Math.random() * sku.length - 1)];
      document.getElementById("message").innerHTML = sku[random];   

      });
        wrap.innerHTML += `<tr>
                             <td>${book.sku}</td>
                             <td>${book.price}</td>
                          </tr>`
   }
content_copy Copy

Tutorial code

https://stackoverflow.com/questions/62475600/how-to-generate-a-random-number-from-an-array