1

I have the following code;

namedarray['India']='New Delhi';
namedarray['Australia']='Canberra';
namedarray['Indonasia']='Jakarta';
namedarray['Iran']='Tehrani';
namedarray['Iraq']='Bhagdad';
namedarray['Nijeria']='Abuja';

document.getElementById('question').innerHTML="Q." +namedarray['Nepal']+"  is capital for which country";

In place of Nepal, I want to choose a key from the object at random. How can I do this?

Andy E
  • 338,112
  • 86
  • 474
  • 445
Mihir
  • 8,696
  • 18
  • 56
  • 87

3 Answers3

4

Try this:

function fetch_random(obj) {
    var temp_key, keys = [];
    for(temp_key in obj) {
       if(obj.hasOwnProperty(temp_key)) {
           keys.push(temp_key);
       }
    }
    return obj[keys[Math.floor(Math.random() * keys.length)]];
}

var random_name = fetch_random(namedarray);
document.getElementById('question').innerHTML="Q." + random_name +"  is capital for which country"
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • Thank you.. but can u explain what is obj here and what is temp_key and why u use keys[] here based on my example plz.. – Mihir Dec 13 '10 at 10:58
  • @Mihir `obj` is the object which is passed in. The way it works is it iterates through all of the properties of the object, and adds that to an array, which we then return a random element of that array of keys, which in turn we pass it to the object to fetch the actual value out. – Jacob Relkin Dec 13 '10 at 10:59
  • @Ivo Yeah, you're right. I edited it out. I completely understand your rage. :) – Jacob Relkin Dec 13 '10 at 11:02
  • @Mihir `keys = []` is assigning a *real array* to a variable called `keys`, so we can then fetch a random key out of it. – Jacob Relkin Dec 13 '10 at 11:07
1

If you are capable of using libraries, you may find that Lo-Dash JS library has lots of very useful methods for such cases. In this case, go ahead and check sample().

(Note Lo-Dash convention is naming the library object _. Don't forget to check installation in the same page to set it up for your project.)

_.sample([1, 2, 3, 4]);
// → 2

In you case, go ahead and use:

_.sample(namedarray)

and in context:

document.getElementById('question').innerHTML="Q." +_.sample(namedarray)+"  is capital for which country";

On a side note, you could use a simpler notation for populating the array.

namedarray = {
    India : 'New Delhi',
    Australia : 'Canberra',
    Indonasia : 'Jakarta',
    Iran : 'Tehrani',
    Iraq : 'Bhagdad',
    Nijeria : 'Abuja'
}
Selfish
  • 6,023
  • 4
  • 44
  • 63
0

I would just use two arrays for the data.

var countries = ['India', 'Australia', 'Indonasia', ... ];
var capitols = ['New Delhi', 'Canberra', 'Jakarta', ...];

Then insert into your text with:

var index = Math.floor(Math.random() * capitols.length);
document.getElementById('question').innerHTML="Q." +capitols[index]+"  is capital for which country";

You can then use the index variable to look up the answer later on as well.

sje397
  • 41,293
  • 8
  • 87
  • 103
  • yes u said correct but it is not applicable to the named arrays.. it is only for ordinary arrays i need answer for named arrays.. thank you so much – Mihir Dec 13 '10 at 10:56