【发布时间】:2014-08-22 19:10:40
【问题描述】:
<html>
<head></head>
<body>
<center>
<div id="output" style="width:1000px; height:800px; background-color:grey; word-wrap: break-word;"></div> //divider is here
</center>
</body>
<script>
var printOut = function(text) {
var output = document.getElementById("output");
output.innerHTML += text;
};
var userChoice = prompt("Do you choose Rock, Paper, or Scissors?")
console.log(printOut("You chose " + userChoice + ".")); //Used here..
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "Rock";
} else if(computerChoice <= 0.67) {
computerChoice = "Paper";
} else {
computerChoice = "Scissors";
} console.log(printOut("The computer chooses " + computerChoice + ".")); //Here too..
var compareChoice = function(userChoice, computerChoice) {
if(userChoice === computerChoice) {
return "The result is a tie!";
}
if (userChoice === "Rock") {
if(computerChoice === "Scissors") {
return "You win! Rock smashes Scissors!";
} else {
return "You lose! Paper covers Rock!";
}
}
if (userChoice === "Paper") {
if(computerChoice === "Rock") {
return "You win! Paper covers Rock!";
} else {
return "You lose! Scissors cut Paper!!"
}
}
if (userChoice === "Scissors") {
if (computerChoice === "Paper") {
return "You win! Scissors cut Paper!";
} else {
return "You lose! Rock smashes Scissors!";
}
}
};
console.log(printOut(compareChoice(userChoice, computerChoice))); //Lastly, here.
</script>
</html>
如何让结果显示在不同的行中,例如:
“你选择了摇滚。
计算机选择了剪刀。
石头砸剪刀。你赢了!”
相对于
“你选择了 Rock。计算机选择了 Scissors。Rock 粉碎了 Scissors。你赢了!”
【问题讨论】:
标签: javascript html function divider