1

I am trying to generate a random SKU number from the array. Using Math.floor() and Math.random() to get the index. But it only returns a letter or number instead of the entire string. Here is the function:

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>`
   }

expected output: 101ZS536

current output: Z

// example of the data 

         [{sku: '101ZS536',
            price: 4.37
          },
          {
            sku: '10134QYT',
            price: 42.61
          },
          {
            sku: '10134QYT',
            price: 53.31
          },
          {
            sku: '10134QYT',
            price: 0
          },
          {
            sku: '10134QYT',
            price: 3.46
          }
        ]

Any advice or suggestions will be highly appreciated.

3
  • What is bookData? Why do you use .forEach()? What is book.sku/sku? If you answer all those questions you should find the source of your problem. Commented Jun 19, 2020 at 17:31
  • Its returning z because sku value is set equal to [1,0,1,Z,S,5,3,6]. i.e. the value of book.sku Commented Jun 19, 2020 at 17:36
  • @Andreas bookData holds the entire data, .forEach() goes over each object in the data and book.sku returns each book sku number from each object. I am trying to retrieve 1 of those sku numbers randomly. Commented Jun 19, 2020 at 19:36

4 Answers 4

1

Check this satisfies your scenario.

function bookRentData() {
var bookData =  [{sku: '101ZS536',
            price: 4.37
          },
          {
            sku: '10134QYT',
            price: 42.61
          },
          {
            sku: '10134QXT',
            price: 53.31
          },
          {
            sku: '10134QZT',
            price: 0
          },
          {
            sku: '10134QST',
            price: 3.43
          },
           {
            sku: '10134QCT',
            price: 3.46
          },
           {
            sku: '10134QYT',
            price: 3.1
          },
           {
            sku: '10134QYT',
            price: 3.2
          },
           {
            sku: '10134QBT',
            price: 3.6
          },
           {
            sku: '10134QYT',
            price: 3.5
          },
          {
            sku: '10134QJT',
            price: 3.1
          },
           {
            sku: '10134QYT',
            price: 3.2
          },
           {
            sku: '10134QRT',
            price: 3.6
          },
           {
            sku: '10134QKT',
            price: 3.5
          }
          
        ];
   book = bookData[Math.floor(Math.random() * bookData.length)]
   var x = document.getElementById("demo")
   x.innerHTML = book.sku;
       
}
<!DOCTYPE html>
<html>
<body>

<p>Test</p>

<button onclick="bookRentData()">Try it</button>

<p id="demo"></p>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but this returns an entire object. I am only trying to retrieve the SKU number from the array of objects. In this case, 10134QYT. Later, I will update the price.
You can take the SKU number from the object, right? Check the updated snippet
0

Here's a simple and readable answer that uses rando.js to access a random value from the array and then accesses the sku of that object:

var arr = [
  {sku: '123ZS536', price: 4.37},
  {sku: '45634ABC', price: 42.61},
  {sku: '10134DEF', price: 53.31},
  {sku: '78934GHI', price: 0},
  {sku: '10134QYT', price: 3.46}
];

console.log( rando(arr).value.sku );
<script src="https://randojs.com/1.0.0.js"></script>

If you want to use this code, don't forget to put that script tag in the head of your html document.

Comments

0

Maybe this will work:

function bookRentData(bookData) {
   book = bookData[Math.floor(Math.random() * bookData.length - 1)]

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

I assume you want a single sku from the list as you need a random one. (And if you want all then no need for them to be random.)

PS comment if I didn't understand your question correctly

2 Comments

this returns the entire object. I am just trying to get the SKU number. Thanks!
Yup book is an object but your inner html will have the no. only as it's book.sku
0

You have Math.floor(Math.random() * sku.length - 1), which might sometimes return -1.

To generate a number between 0 to length, you can change to:

      var random = sku[Math.floor(Math.random() * sku.length)];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.