有人能告诉我这段代码错在哪里吗?它应该是一个简单的问题/答案提示,其中包含基于答案的各种路线。只是预警我是编码菜鸟,所以提前谢谢你帮助我!
var answer = prompt("Would you like to visit the Longneck or T-rex exhibit first?");
var enter = prompt("Welcome to Jurassic Park, are you ready for an adventure?");
if (enter == "yes"){
console.log("Ok, lets go!");
}
else{
console.log("Well too bad, we're already here.");
}
var exhibit = prompt("Would you like to visit the T-rex or the Raptor exhibit first?");
if( exhibit == "T-rex"){
console.log("Good choice, its actually feeding time... So exciting!");
}
var trex = prompt("Wow look at the T-Rex, it's right next to the fenc... OMG it just broke out!! Should we get out of the car and hide or stay in the car and try and drive away. Answer run or drive.");
if(trex == "drive"){
console.log("Good choice! Lets break off the track and drive toward the basecamp!");
}
var survived = prompt("You made it back to basecamp but the t-rex is on your ass! Do you run inside or run and hide in the jungle? Answer inside or jungle.");
if(survived == "inside"){
console.log("Nice you got in and locked the door, after a while the t-rex leaves and your helicopter rescue comes to the rescue. You survived this nightmare.");
}
else(survived == "jungle"){
console.log("The t-rex chases you down and you get eaten. You died a horribly gruesome death.")
}
else(trex == "run"){
console.log("The T-rex chases your group down and you die a horrible death")
}
else(exhibit == "Raptor"){
console.log("Nice! The raptors are some of our smartest dinosaurs");
}
var raptor = prompt("You made it to the raptor cage but as soon as you drive up you see the fence has been slashed open. Do you stay on the track that your automatically driving on or do you bust off the track and drive away as fast as you can? Answer stay or drive.")
if(raptor == "drive"){
console.log("You drive as fast you can but you are noticed by the raptors. They start to chase after you!")
}
var run = prompt("You get back to the basecamp but the raptors chased you all the way. Do you run inside and lock yourself in or run to the jungle and try to hide? Answer inside or jungle.")
if(run == "inside"){
console.log("You succesfully run inside and lock the door before the raptors can get you. They try for hours to get in but then run off. Your rescue helicopter comes and you survive.")
}
else(run == "jungle"){
console.log("The raptor smell you out and rip you and your friends into a million pieces and eat all of ya'll. You died a terrible death.")
}
else(raptor == "stay"){
console.log("You drive slowly passed the fence and immediately get noticed by the raptors. They claw their way in your car and eat you and all of you group.")
})

答案 0 :(得分:1)
else(exhibit == "Raptor")
else(run == "jungle")
和
else(raptor == "stay")
和其他两个这样的行是无效的陈述。它应该是else if(
... )
。
控制台输出显示
SyntaxError:missing;在陈述之前
对于您的情况,它没有意义,但您必须首先确保您的语法正确,以避免此错误消息。 要验证JS代码,您可以使用JSHint。
显然,最后)
也应该删除。
然后你只需要检查你的逻辑 - 我。即您的if
,else if
和else
块,以确认程序流程(决策和prompt
s等)是否符合您的预期(因为,正如jsve所说,你的一些if
块太快关闭了。
答案 1 :(得分:1)
您过早关闭if语句。
var answer = prompt("Would you like to visit the Longneck or T-rex exhibit first?");
var enter = prompt("Welcome to Jurassic Park, are you ready for an adventure?");
if (enter === "yes") {
console.log("Ok, lets go!");
}
else {
console.log("Well too bad, we're already here.");
}
var exhibit = prompt("Would you like to visit the T-rex or the Raptor exhibit first?");
if (exhibit === "T-rex") {
console.log("Good choice, its actually feeding time... So exciting!");
// <-- NO Close brace here, that way, the code below (until the brace will
// only be executed when exhibit === "T-rex"
var trex = prompt("Wow look at the T-Rex, it's right next to the fenc... OMG it just broke out!! Should we get out of the car and hide or stay in the car and try and drive away. Answer run or drive.");
if (trex === "drive"){
console.log("Good choice! Lets break off the track and drive toward the basecamp!");
// <-- No close brace here either
var survived = prompt("You made it back to basecamp but the t-rex is on your butt! Do you run inside or run and hide in the jungle? Answer inside or jungle.");
if (survived === "inside") {
console.log("Nice you got in and locked the door, after a while the t-rex leaves and your helicopter rescue comes to the rescue. You survived this nightmare.");
} else(survived == "jungle") {
console.log("The t-rex chases you down and you get eaten. You died a horribly gruesome death.")
}
} // <-- insert close brace here. This ends the if (trex === "drive")
// scenario
else(trex == "run") {
console.log("The T-rex chases your group down and you die a horrible death")
}
// <-- insert close brace here. This ends the if (exhibit === "T-rex")
// scenario
else if (exhibit === "Raptor") { // <-- need to use "else if" here
console.log("Nice! The raptors are some of our smartest dinosaurs");
// <-- no close brace here
var raptor = prompt("You made it to the raptor cage but as soon as you drive up you see the fence has been slashed open. Do you stay on the track that your automatically driving on or do you bust off the track and drive away as fast as you can? Answer stay or drive.")
if (raptor === "drive") {
console.log("You drive as fast you can but you are noticed by the raptors. They start to chase after you!")
}
var run = prompt("You get back to the basecamp but the raptors chased you all the way. Do you run inside and lock yourself in or run to the jungle and try to hide? Answer inside or jungle.")
if (run === "inside") {
console.log("You successfully run inside and lock the door before the raptors can get you. They try for hours to get in but then run off. Your rescue helicopter comes and you survive.")
}
else if (run === "jungle") { // <-- need to use "else if" here
console.log("The raptor smell you out and rip you and your friends into a million pieces and eat all of ya'll. You died a terrible death.")
}
else if (raptor === "stay") { // <-- need to use "else if" here too
console.log("You drive slowly passed the fence and immediately get noticed by the raptors. They claw their way in your car and eat you and all of you group.")
} // <-- no ")" here, there's no "(" to match it.
} // <-- need a closing else if (exhibit === "Raptor") scenario
其他说明:
===
代替==
。 (见这个问题:Which equals operator (== vs ===) should be used in JavaScript comparisons?)我修复了一些(但不是全部)拼写和格式问题。我建议使用某种类型的IDE来帮助您正确地格式化代码,这样可以更容易地在代码中查找错误。
正如Xufox中his answer所述,您可以使用JSHint验证您的JavaScript。
答案 2 :(得分:0)
你过早地关闭你的if和else语句而你错过了'if'in'else if'...
var answer = prompt("Would you like to visit the Longneck or T-rex exhibit first?");
var enter = prompt("Welcome to Jurassic Park, are you ready for an adventure?");
if (enter == "yes"){
console.log("Ok, lets go!");
}
else {
console.log("Well too bad, we're already here.");
}
var exhibit = prompt("Would you like to visit the T-rex or the Raptor exhibit first?");
if( exhibit == "T-rex"){
console.log("Good choice, its actually feeding time... So exciting!");
var trex = prompt("Wow look at the T-Rex, it's right next to the fenc... OMG it just broke out!! Should we get out of the car and hide or stay in the car and try and drive away. Answer run or drive.");
if(trex == "drive"){
console.log("Good choice! Lets break off the track and drive toward the basecamp!");
var survived = prompt("You made it back to basecamp but the t-rex is on your ass! Do you run inside or run and hide in the jungle? Answer inside or jungle.");
if(survived == "inside"){
console.log("Nice you got in and locked the door, after a while the t-rex leaves and your helicopter rescue comes to the rescue. You survived this nightmare.");
}
else if(survived == "jungle"){
console.log("The t-rex chases you down and you get eaten. You died a horribly gruesome death.")
}
}
else if(trex == "run"){
console.log("The T-rex chases your group down and you die a horrible death")
}
}
else if(exhibit == "Raptor"){
console.log("Nice! The raptors are some of our smartest dinosaurs");
var raptor = prompt("You made it to the raptor cage but as soon as you drive up you see the fence has been slashed open. Do you stay on the track that your automatically driving on or do you bust off the track and drive away as fast as you can? Answer stay or drive.")
if(raptor == "drive"){
console.log("You drive as fast you can but you are noticed by the raptors. They start to chase after you!")
var run = prompt("You get back to the basecamp but the raptors chased you all the way. Do you run inside and lock yourself in or run to the jungle and try to hide? Answer inside or jungle.")
if(run == "inside"){
console.log("You succesfully run inside and lock the door before the raptors can get you. They try for hours to get in but then run off. Your rescue helicopter comes and you survive.")
}
else if(run == "jungle"){
console.log("The raptor smell you out and rip you and your friends into a million pieces and eat all of ya'll. You died a terrible death.")
}
}
else if(raptor == "stay"){
console.log("You drive slowly passed the fence and immediately get noticed by the raptors. They claw their way in your car and eat you and all of you group.")
})
}