2016-01-01 54 views
-1

我一直在尝试这个,它仍然说这是错误的。它一直说numberOfRounds是未定义的。有另一种方法可以做到吗?我希望它运行“选择___颜色”语句的用户需要的次数。如何将用户输入存储在变量中?

<html> 
 
<!--This is the start screen that you see to begin the game. You also select how many rounds of the game you would like.--> 
 
<font size="6"><center><strong><p id="startScreen">Welcome to the Random Color Game.</p></strong></center></font> 
 
<font size="4"><center><p id="startScreen2">How many many rounds of the game would you like?</p> 
 
<form id="numberOfRounds"><p id="startScreen3">I would like <input id="numberOfRounds" type="number" min="1" max="20" name="numberOfRounds"> rounds.</p> 
 
<p id="startScreen5">To start playing the game, push begin.</p></center></font> 
 

 
<center><h4 id="sayColor"></h4></center> 
 

 

 
<center><p id="startButton"><button type="button" onclick="startGame();buttonColors();">Begin</button></p></center> 
 
<!--this is the paragraph that will have all the buttons placed inside.--> 
 
<center><p id="game"></p><center> 
 

 
<script> 
 
\t 
 
\t var randomNumber = 0; 
 
\t var redPressed = false; 
 
\t var bluePressed = false; 
 
\t var greenPressed = false; 
 
\t var purplePressed = false; 
 
\t var orangePressed = false; 
 
\t var x = 1; 
 

 
function startGame() 
 
{ 
 
\t buttonColors(); 
 
\t //gets rid of the start screen text 
 
\t document.getElementById("startScreen").innerHTML = ""; 
 
\t document.getElementById("startScreen2").innerHTML = ""; 
 
\t document.getElementById("startScreen3").innerHTML = ""; 
 
\t document.getElementById("startScreen5").innerHTML = ""; 
 
\t //makes the text for the game 
 
\t document.getElementById("startButton").innerHTML = "<button type='button' onclick='location.reload()'>Restart</button>"; 
 
\t document.getElementById("game").innerHTML = "<button type='button' onclick='redPressed = true;redCheck();' style='background-color:red'>Red</button><button type='button' onclick='bluePressed = true;blueCheck();' style='background-color:blue'>Blue</button><button type='button' onclick='greenPressed = true;greenCheck();' style='background-color:green'>Green</button><button type='button' onclick='purplePressed = true;purpleCheck();' style='background-color:purple'>Purple</button><button type='button' onclick='orangePressed = true;orangeCheck();' style='background-color:orange'>Orange</button>"; 
 
\t //checks to see if the function ran 
 
\t console.log("startGame() ran."); 
 
\t makeRounds(); 
 
} 
 

 
function makeRounds() 
 
{ 
 
\t //takes the number of rounds and puts it into a variable so it shows the amount of questions you want 
 
\t var numberOfRounds = document.getElementById("numberOfRounds").value; 
 
\t console.log(numberOfRounds); 
 
\t x = numberOfRounds*2; 
 
\t console.log(x); 
 
\t while (x<=numberOfRounds) 
 
\t { 
 
\t \t randomNumber = (Math.floor(Math.random() * 5)); 
 
\t \t console.log(randomNumber); 
 
\t } 
 
} 
 

 
function buttonColors() 
 
{ 
 
\t if (randomNumber==1) 
 
\t { 
 
\t \t document.getElementById("sayColor").innerHTML = "Push the red button." 
 
\t } 
 
\t if (randomNumber==2) 
 
\t { 
 
\t \t document.getElementById("sayColor").innerHTML = "Push the blue button." 
 
\t } 
 
\t if (randomNumber==3) 
 
\t { 
 
\t \t document.getElementById("sayColor").innerHTML = "Push the green button." 
 
\t } 
 
\t if (randomNumber==4) 
 
\t { 
 
\t \t document.getElementById("sayColor").innerHTML = "Push the purple button." 
 
\t } 
 
\t if (randomNumber==5) 
 
\t { 
 
\t \t document.getElementById("sayColor").innerHTML = "Push the orange button." 
 
\t } 
 
} 
 

 
function redCheck() 
 
{ 
 
\t if (randomNumber==1) 
 
\t { 
 
\t \t correct(); 
 
\t } 
 
\t else 
 
\t { 
 
\t \t incorrect(); 
 
\t } 
 
\t x--; 
 
} 
 

 
function blueCheck() 
 
{ 
 
\t if (randomNumber==2) 
 
\t { 
 
\t \t correct(); 
 
\t } 
 
\t else 
 
\t { 
 
\t \t incorrect(); 
 
\t } 
 
\t x--; 
 
} 
 

 
function greenCheck() 
 
{ 
 
\t if (randomNumber==3) 
 
\t { 
 
\t \t correct(); 
 
\t } 
 
\t else 
 
\t { 
 
\t \t incorrect(); 
 
\t } 
 
\t x--; 
 
} 
 

 
function purpleCheck() 
 
{ 
 
\t if (randomNumber==4) 
 
\t { 
 
\t \t correct(); 
 
\t } 
 
\t else 
 
\t { 
 
\t \t incorrect(); 
 
\t } 
 
\t x--; 
 
} 
 

 
function orangeCheck() 
 
{ 
 
\t if (randomNumber==5) 
 
\t { 
 
\t \t correct(); 
 
\t } 
 
\t else 
 
\t { 
 
\t \t incorrect(); 
 
\t } 
 
\t x--; 
 
} 
 

 
function correct() 
 
{ 
 
\t console.log("DATS RIGHT!!"); 
 
\t window.alert("Correct!") 
 
} 
 

 
function incorrect() 
 
{ 
 
\t console.log("Incorrect."); 
 
\t window.alert("Incorrect."); 
 
} 
 

 
</script> 
 

 

 

 
</html>

+0

您有'numberOfRounds' id的多个声明。 – Cyval

+0

您的startGame()函数会清除startScreen3中包含numberOfRounds元素的内容。在删除内容之前捕获这些数据。 – jeff

回答

1
<form id="numberOfRounds"><p id="startScreen3">I would like <input id="numberOfRounds" type="number" min="1" max="20" name="numberOfRounds"> rounds.</p> 

两者有相同的id,numberOfRounds。

相关问题