JavaScriptで簡単にバリューをフィフティ・フィフティに分ける方法
二者択一の機能を実装する必要がある場合、ランダムで0か1をレスポンスで返す機能が役に立ちます。そうコイントスのような感じで。
function coinFlip() {
return Math.floor(Math.random() * 2);
}
True または, Falseのバリューで返したいときは、
function coinFlip() {
return (Math.floor(Math.random() * 2) == 0);
}
または、本当にコイントスのような結果を返したいときは、、、
function coinFlip() {
return (Math.floor(Math.random() * 2) == 0) ? '裏' : '表';
}
役に立ちましたか?
Written by STK
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Math
Authors
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#