Tyler Ramsey
Tyler Ramsey

Reputation: 31

Picking a Random Variable out of an Array in Javascript

I can't figure out what my problem is here. I'm working on this in jsfiddle. I have a div in html that's just a box and I want to append a string to it from a variable using jquery.

var spongebob='pineapple';
var patrick='rock';
var sandy = 'air dome';
var mrKrabs = 'anchor';
var house = [spongebob, patrick, sandy, mrKrabs];
var pickHouse= house[Math.floor(Math.random * house.length)];
$('div').append(pickHouse);

I have a feeling my problem is in my second to last line but I can't figure out what it is.

Upvotes: 0

Views: 29

Answers (1)

Secret
Secret

Reputation: 3338

var spongebob='pineapple';
var patrick='rock';
var sandy = 'air dome';
var mrKrabs = 'anchor';
var house = [spongebob, patrick, sandy, mrKrabs];
var pickHouse= house[Math.floor(Math.random() * house.length)];
$('div').append(pickHouse);

You forgot the parenthesis in Math.random() (hence getting NaN for the index)

Upvotes: 1

Related Questions