我刚开始学习JavaScript。我试着做一个简单的游戏,看看能不能。但我不断收到错误。我修复了一个,但我得到另一个,所以有人可以帮我调试一下吗? 我知道我错过了一些简单的事情。
现在我得到了:
SyntaxError: Unexpected identifier
这是我的代码:
confirm("Are you ready to play 'I CAN GUESS THAT'? A game where Player 2 tries to guess player 1 number?");
//find out names
var player1 = prompt("Player 1 what is your name?","Your name here");
var player2 = prompt("Player 2 what is your name?","Your name here");
//player 1 number
var p1 =place_holder
while(p1 <1000000) {
var p1 = prompt("Player 2 look away." + " " + player1 + " " + "what is your number?", "Your number under 1,000,000 here");
};
//set up used guess list
var listlow = []
var listhigh = []
var x = 0
var p2 =place_holder
//game
while( x <11) {
//list used guesses
console.log("Your guess so far");
console.log("Your to low guesses" + listlow);
console.log("Your to high gusses" + highlist);
//player 2 guess
var p2= prompt("Player 2 what is your guess?");
//good guess
var test = p1/p2
if (test === 1){
console.log("Congrats" + " " + player2 + "You have guessed" + " " + player1 + "number");
var x = 30;
//to low
}if (test > 1) {
console.log(player2 + "Sorry your guess is to low");
listlow.push(p2);
x=x++;
//to high
}else if(test <1){
console.log(player2 + "Sorry your guess is to high");
listhigh.push(p2);
x=x++;
//something went wrong
}else if
console.log("Opps something went wrong");
};
console.log("Thanks for playing")
编辑:我将else更改为if else,我仍然收到该错误
答案 0 :(得分:1)
如果出现以下情况,您需要将else更改为else:
var test = p1/p2;
if (test === 1){
console.log("Congrats" + " " + player2 + "You have guessed" + " " + player1 + "number");
var x = 30;
//to low
}else if(test > 1) {
console.log(player2 + "Sorry your guess is to low");
listlow.push(p2);
x=x++;
//to high
} else if (test <1){
console.log(player2 + "Sorry your guess is to high");
listhigh.push(p2);
x=x++;
//something went wrong
}else{
console.log("Opps something went wrong");
}
};
答案 1 :(得分:0)
if else
语法不正确;你应该有else if
:
if (test === 1){
console.log("Congrats" + " " + player2 + "You have guessed" + " " + player1 + "number");
var x = 30
//to low
} else if(test > 1) {
console.log(player2 + "Sorry your guess is to low");
listlow.push(p2);
x=x++;
//to high
} else if(test <1){
console.log(player2 + "Sorry your guess is to high");
listhigh.push(p2);
x=x++;
//something went wrong
} else{
console.log("Opps something went wrong");
}
答案 2 :(得分:0)
否则不需要()
。只有声明得到了parens。
答案 3 :(得分:0)
您的所有if else
语句都应为else if
语句,;
之后需要var x = 30
confirm("Are you ready to play 'I CAN GUESS THAT'? A game where Player 2 tries to guess player 1 number?");
//find out names
var player1 = prompt("Player 1 what is your name?","Your name here");
var player2 = prompt("Player 2 what is your name?","Your name here");
//player 1 number
var place_holder = 0;
var p1 =place_holder;
while(p1 > 1000000 || p1 == 0) {
p1 = prompt("Player 2 look away." + " " + player1 + " " + "what is your number?", "Your number under 1,000,000 here");
if (p1 != parseInt(p1)){
p1 = 0;
}
};
//set up used guess list
var listlow = [];
var listhigh = [];
var x = 0;
var p2 =place_holder;
//game
while( x <11) {
//list used guesses
console.log("Your guess so far");
console.log("Your to low guesses" + listlow);
console.log("Your to high gusses" + highlist);
//player 2 guess
var p2= prompt("Player 2 what is your guess?");
//good guess
var test = p1/p2;
if (test === 1){
console.log("Congrats" + " " + player2 + "You have guessed" + " " + player1 + "number");
var x = 30;
//to low
}else if(test > 1) {
console.log(player2 + "Sorry your guess is to low");
listlow.push(p2);
x=x++;
//to high
}else if(test <1){
console.log(player2 + "Sorry your guess is to high");
listhigh.push(p2);
x=x++;
//something went wrong
}else{
console.log("Opps something went wrong");
}
};
console.log("Thanks for playing")
答案 4 :(得分:0)
这可能与您上一次else if
声明有关。没有条件可以评估!
可能期待else if(//some sort of condition)
,但你写了else if
没有条件。