mercredi 30 janvier 2019

Accessing Random Property of Object

I have a huge object that contains the entire ESV Bible. I am trying to get a random book of the Bible whenever I press a button. Here is a small sample of the object.

var esvObject = {

 "Genesis": {

    "1": {}
    "2": {}
    "3": {}

   },
 "Exodus": {

    "1": {}
    "2": {}
    "3": {}

   }

}

Here is the code that I am using to get a random book.

var randNum = Math.floor(Math.random() * 66);

var randBook = esvObject[randNum];

var output = randBook;

I would expect this to work, but the output I always get is "undefined," where as I would like the output to be "Genesis" or "Exodus" etc. Getting the random number works, but if

randNum = 1

then

randBook = esvObject[1]

the output returns as "undefined" and I don't know why.




Aucun commentaire:

Enregistrer un commentaire