Reputation: 117
Suceeded in randomizing a quote but now the values name and quote don't match, is there a way to make json.name
and json.quote
share the same random value? so Einstein's quote isn't matched with the name Nightingale.
let url =;
$.getJSON( url, function( json ) {
let rand = function getRandom() {
return Math.floor(Math.random() * 4) + 1 ;
}
//console.log(rand());
document.write(" "" + json[rand()].quote +""" + " by "
+json[rand()].name );
});
Upvotes: 2
Views: 391
Reputation: 9808
document.write(" "" + json[rand()].quote +""" + " by "
+json[rand()].name );
everytime you use rand()
, it will generate a different number, so json[rand()].quote
and json[rand()].name
will be different because you are using different keys, so save rand()
in a variable first and then use it, like:
var randNumber = rand();
document.write(" "" + json[randNumber].quote +""" + " by " +json[randNumber].name );
Upvotes: 5