我创建了一个基本的石头剪刀游戏,我无法在结果中返回结果。
我一直在尝试一切
链接控制台记录比较并将其放入变量..
console.log(compare)由于某种原因返回undefined。
请帮忙
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
console.log("Your choice: " + userChoice);
console.log(computerChoice + " vs " + userChoice);
var compare = function(userChoice, computerChoice){
if (userChoice === computerChoice){
return "The result is a tie!";
} else if (userChoice ==="rock"){
if(computerChoice ==="scissors"){
return "rock wins!";
}
else {
return "paper wins!";
}
} else if (userChoice === "paper") {
if(computerChoice === "rock") {
return "paper wins!";
}
else {
return "scissors wins!";
}
} else if (userChoice === "scissors") {
if(computerChoice === "rock") {
return "rock wins!";
}
else {
return "scissors wins!";
}
}
}
&#13;
答案 0 :(得分:2)
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
console.log("Your choice: " + userChoice);
console.log(computerChoice + " vs " + userChoice);
function compare(userChoice, computerChoice){
if (userChoice === computerChoice){
return "The result is a tie!";
} else if (userChoice ==="rock"){
if(computerChoice ==="scissors"){
return "rock wins!";
}
else {
return "paper wins!";
}
} else if (userChoice === "paper") {
if(computerChoice === "rock") {
return "paper wins!";
}
else {
return "scissors wins!";
}
} else if (userChoice === "scissors") {
if(computerChoice === "rock") {
return "rock wins!";
}
else {
return "scissors wins!";
}
}
}
var Compare = compare(userChoice, computerChoice);
console.log(Compare);
答案 1 :(得分:0)
这里有语法错误...
return = "The result is a tie!";
将其更改为:
return "The result is a tie!";
答案 2 :(得分:0)
正如Shinra tensei提到你在第18行中有=
导致错误。
这是一个更新的剪辑。我还为比较功能添加了一个警报。
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
console.log("Your choice: " + userChoice);
console.log(computerChoice + " vs " + userChoice);
alert(compare(userChoice,computerChoice));
function compare (userChoice, computerChoice){
if (userChoice === computerChoice){
return "The result is a tie!";
} else if (userChoice ==="rock"){
if(computerChoice ==="scissors"){
return "rock wins!";
}
else {
return "paper wins!";
}
} else if (userChoice === "paper") {
if(computerChoice === "rock") {
return "paper wins!";
}
else {
return "scissors wins!";
}
} else if (userChoice === "scissors") {
if(computerChoice === "rock") {
return "rock wins!";
}
else {
return "scissors wins!";
}
}
}
&#13;
答案 3 :(得分:0)
这里的主要问题是你从未执行过你写的匿名函数。我比较了一个普通函数而不是匿名函数,只是为了让它更像其他语言。这是一个很好的工作代码:
div > span:last-of-type {
display:table-row!important;
}