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';
精彩评论