开发者

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

开发者 https://www.devze.com 2023-01-21 04:29 出处:网络
Given an Array of two elements what is the most elegant way to express a random coin toss to choose between them?

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

精彩评论

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