开发者

Most elegant way to express random choice between two options in JavaScript

Given an Array of two elements what is the most elegant way to express a random coin toss to choose between them?

var choices = ['#00f', '#ff0'];
documen开发者_开发知识库t.bgcolor = choices[Math.floor(Math.random() * choices.length)];


document.bgColor = (Math.random() < 0.5) ? '#0000FF' : '#FF0000';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜