声明数组JavaScript时出错?
完成JS课程简介课程。我正在做一个简单的颜色猜测游戏。我在开始时插入了一些警报以进行故障排除,但我无法让此程序访问警报(“test1”);我的游戏中的线,它通过身体元素立即加载。知道我做错了什么吗?我似乎在宣布一切都很好声明数组JavaScript时出错?,javascript,Javascript,完成JS课程简介课程。我正在做一个简单的颜色猜测游戏。我在开始时插入了一些警报以进行故障排除,但我无法让此程序访问警报(“test1”);我的游戏中的线,它通过身体元素立即加载。知道我做错了什么吗?我似乎在宣布一切都很好 <!DOCTYPE html> <html> <body onload="playGame()"> <p>Welcome to my color guessing game</p> <script languag
<!DOCTYPE html>
<html>
<body onload="playGame()">
<p>Welcome to my color guessing game</p>
<script language = "JavaScript">
function inArray(needle, haystack){
for (var i = 0; i <haystack.length(); i++){
if (needle === haystack[i]){
return true;
}
}
return false;
}
function changeBackground(color){
document.body.style.background = color;
}
function playGame(){
var correct = false;
alert("Correct status is: " + correct);
var colorArray = ["cyan", "gold", "green", "gray", "magenta", "blue", "red", "orange", "yellow", "white"];
alert("test1");
for (var i = 0; i < colorArray.length(); i++){
alert("for " + i + " the color is " + colorArray[i]);
}
colorArray = colorArray.sort();
alert("answer index is " + answerIndex);
alert("color array length is " + colorArray.length());
var answerIndex = Math.floor(Math.random()*colorArray.length());
alert("the resulting color from the color array is: " + answerColor);
var answerColor = colorArray[answerIndex];
alert("The correct color is " + answerColor);
var answerList = colorArray.join(", ");
var guessCount = 0;
while(!correct){
var colorGuess = prompt("Welcome to my guessing game! The colors available for your choosing are: " + "\n\n" + answerList + "\n\n" + "Which color am I thinking of?");
guessCount++;
if (!inArray(colorGuess, colorArray)){
alert("Your guess wasn't one of the selections that was available or I otherwise don't recognize it." + "\n\n" + "Please try again!");
}
else{
if (colorArray.indexOf(colorGuess)<color.indexOf(answerColor)){
alert("Your guess was alphabetically before the correct color! Try again.");
}
else if (colorARray.indexOf(colorguess)>color.indexOf(answerColor)){
alert("Your guess was alphabetically after the correct color! Try again.");
}
else{
alert("Your guess is correct!");
correct = true;
changeBackground(answerColor);
}
}
}
alert("Great job!" + "\n\n" + "You took " + guessCount + " guesses to get the correct answer!");
}
</script>
</body>
</html>
欢迎来到我的颜色猜谜游戏
功能(针、干草堆){
对于(var i=0;i我刚刚测试了您的代码
alert("answer index is " + answerIndex);
alert("color array length is " + colorArray.length());
var answerIndex = Math.floor(Math.random()*colorArray.length());
alert("the resulting color from the color array is: " + answerColor);
var answerColor = colorArray[answerIndex];
alert("The correct color is " + answerColor);
在这里,您试图在声明变量之前警告变量answerIndex
。在JSlength
中,它是一个返回数组中元素数的属性
在colorArray.length()
replacelength()
bylength中,有许多愚蠢的javascript语法错误,修复了所有问题,下面是正在运行的代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body onload="playGame()">
<p>Welcome to my color guessing game</p>
<script type="text/javascript">
function inArray(needle, haystack){
for (var i = 0; i <haystack.length; i++){
if (needle === haystack[i]){
return true;
}
}
return false;
}
function changeBackground(color){
document.body.style.background = color;
}
function playGame(){
var correct = false;
alert("Correct status is: " + correct);
var colorArray = ["cyan", "gold", "green", "gray",
"magenta", "blue", "red", "orange", "yellow",
"white"];
alert("test1" + colorArray.length);
for (var i = 0; i < colorArray.length; i++){
alert("for " + i + " the color is " +
colorArray[i]);
}
colorArray = colorArray.sort();
alert("answer index is " + answerIndex);
alert("color array length is " +
colorArray.length);
var answerIndex =
Math.floor(Math.random()*colorArray.length);
alert("the resulting color from the color array
is: " + answerColor);
var answerColor = colorArray[answerIndex];
alert("The correct color is " + answerColor);
var answerList = colorArray.join(", ");
var guessCount = 0;
while(!correct){
var colorGuess = prompt("Welcome to my guessing
game! The colors available for your choosing
are: " + "\n\n" + answerList + "\n\n" + "Which
color am I thinking of?");
guessCount++;
if (!inArray(colorGuess, colorArray)){
alert("Your guess wasn't one of the selections
that was available or I otherwise don't
recognize it." + "\n\n" + "Please try
again!");
}
else{
if (colorArray.indexOf(colorGuess)
<colorArray.indexOf(answerColor)){
alert("Your guess was alphabetically
before the correct color! Try
again.");
}
else if(colorArray.indexOf(colorGuess) >
colorArray.indexOf(answerColor)){
alert("Your guess was alphabetically after the
correct color! Try again.");
}
else{
alert("Your guess is correct!");
correct = true;
changeBackground(answerColor);
}
}
}
alert("Great job!" + "\n\n" + "You took " + guessCount + "
guesses to get the correct answer!");
}
</script>
</body>
</html>
欢迎来到我的颜色猜谜游戏
功能(针、干草堆){
对于(var i=0;i),这更像是对我的注释。这只会提醒未定义的
,它不会阻止函数继续。另外,请检查console.length()=>.length“…无法…到达提醒(“test1”);
行”你是说它确实会在var colorArray=…
行之前显示警报,但不会在后面显示警报?你应该查看控制台并检查错误。如果你无法解决问题,请编辑问题,以便我们能够清楚地看到你的问题。@AnandSingh你说得对:)--我已经有一段时间没有回到编码上来了,所以。有人能告诉我我的答案出了什么问题,为什么被否决了吗?如果你觉得我的答案有帮助,请把它标记为正确答案-@ssilverWould有人能告诉我我的答案出了什么问题,为什么被否决了吗?