私はcodecademyでDragon Slayer Interactiveアクティビティを完了しました。私は "Math.floor(Math.random()* 2);"そうするはずです。ドラゴンスレイヤー - Math.floor(Math.random()* 2)の意味は?
私の "Dragon Slayer!"対話型活動の完全なコードはここにあります。
var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var totalDamage = 0;
while (slaying) {
var damageThisRound = Math.floor(Math.random() * 5 + 1);
if (youHit) {
console.log(“You hit the dragon!”);
totalDamage += damageThisRound;
if (totalDamage >= 4) {
console.log(“You slew the dragon!”);
slaying = false;
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log(“The dragon defeated you!”);
slaying = false;
}
}
Math.floor(Math.random()* 2)の機能は何ですか?
ランダムに番号「0」または「1」のいずれかを生成します。 'Math.random()'がどのように動作するかを調べることをお勧めします。 – abhishekkannojia
キャメロンを覚えている**本当の**ドラゴンスレイヤー**は彼の質問を適切にフォーマットして、みんながあなたの** [MCVE](https://stackoverflow.com/help/mcve)**を簡単に読むことができるようにします。奴隷化し続ける! – iamdanchiv
[Concept of Math.floor(Math.random()\ * 5 + 1)の可能な複製は、真の範囲とその理由は何ですか?](https://stackoverflow.com/questions/21483667/concept-of-math) -floormath-random-5-1-真実の範囲と理由) –