这是我的第一款游戏和Rock Paper Scissors游戏的代码,其中包含CodeAcademy的一些调整。我已经被困了两个小时试图找出它为什么会导致平局。
这是我的HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Rock Paper Scissors</title>
</head>
<body>
<form>
Rock
<input type="checkbox" id="rock">
Paper
<input type="checkbox" id="paper">
Scissors
<input type="checkbox" id="scissors">
<button onclick="startGame()">Start<button>
</form>
<script src="userInt.js"></script>
</body>
</html>
Javascript代码:
function userChoice(){
var rock = document.getElementById("rock").checked;
var scissors = document.getElementById("scissors").checked;
var paper = document.getElementById("paper").checked;
}
function computerChoice(){
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
}
function compare(userChoice, computerChoice){
if (userChoice === computerChoice){
return "The result is a tie!";
}
if (userChoice ==="rock"){
if(computerChoice ==="scissors"){
return "rock wins";
}else{
return "paper wins";
}
}
if(userChoice ==="paper"){
if (computerChoice ==="scissors"){
return "paper wins"
}else{
return "scissors wins"
}
}
if(userChoice ==="scissors"){
if (computerChoice ==="rock"){
return "rock wins"
}else{
return "scissors wins"
}
}
}
function startGame(){
var getUserChoice = userChoice();
var getComputerChoice = computerChoice();
var endGame = compare(getUserChoice, getComputerChoice);
alert(endGame)
}
感谢您的支持!
答案 0 :(得分:1)
您的userChoice()和computerChoice()不返回任何内容。
function userChoice() {
var choice;
['rock', 'scissors', 'paper'].some(function (e) {
if (document.getElementById(e).checked) {
choice = e;
return true;
}
});
return choice;
}
function computerChoice() {
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
return computerChoice;
}
答案 1 :(得分:1)
您的代码中有2个问题:
userChoice
不返回任何内容(undefined
)computerChoice
不返回任何内容(undefined
)function userChoice() {
var rock = document.getElementById("rock").checked;
var scissors = document.getElementById("scissors").checked;
var paper = document.getElementById("paper").checked;
if (rock) {
return 'rock';
}
if (scissors) {
return 'scissors';
}
if (paper) {
return 'paper';
}
}
function computerChoice() {
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
return computerChoice;
}
function compare(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return "The result is a tie!";
}
if (userChoice === "rock") {
if (computerChoice === "scissors") {
return "rock wins";
} else {
return "paper wins";
}
}
if (userChoice === "paper") {
if (computerChoice === "scissors") {
return "paper wins"
} else {
return "scissors wins"
}
}
if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "rock wins"
} else {
return "scissors wins"
}
}
}
function startGame() {
var getUserChoice = userChoice();
console.log(getUserChoice);
var getComputerChoice = computerChoice();
console.log(getComputerChoice);
var endGame = compare(getUserChoice, getComputerChoice);
alert(endGame);
}