Javascript新手-使用JSFIDLE,缺少分号,但我没有';我不完全明白在哪里
好的,我正在使用Javascript通过确认函数、提示符和if-else语句创建一个小型业余游戏。 但是,在我的代码中,您可以在这里找到: 第21行和第29行有一个错误,因为缺少分号。我做错了什么?我真的不知道JS的同谋,所以我很迷茫! 感谢您的帮助Javascript新手-使用JSFIDLE,缺少分号,但我没有';我不完全明白在哪里,javascript,jsfiddle,Javascript,Jsfiddle,好的,我正在使用Javascript通过确认函数、提示符和if-else语句创建一个小型业余游戏。 但是,在我的代码中,您可以在这里找到: 第21行和第29行有一个错误,因为缺少分号。我做错了什么?我真的不知道JS的同谋,所以我很迷茫! 感谢您的帮助 更新 根据GitaarLAB的回答,我创建了以下代码: 你怎么看?例如: if (prompt('What are you planning to do? 1.Stay calm and keep everybody in check. or 2
更新
根据GitaarLAB的回答,我创建了以下代码: 你怎么看?例如:
if (prompt('What are you planning to do? 1.Stay calm and keep everybody in check. or 2. Pacnic') === '1.');
简化版:
if (condition);
If语句结尾没有分号。
PS:几行之后你也犯了同样的错误。例如:
if (prompt('What are you planning to do? 1.Stay calm and keep everybody in check. or 2. Pacnic') === '1.');
简化版:
if (condition);
If语句结尾没有分号。
PS:几行之后你也犯了同样的错误。在你的代码中有很多错误 除了弗朗西斯科·阿丰索在回答中指出的一切之外,我想补充以下几点 最后一行:
window.onload=code地址代码>
在你的小提琴中,你已经选择了“onload”选项(它已经自动包装你的javascript代码)
在else案例中,您有双块{statements here}
您在if
语句中提示了一个问题,然后再次提示相同的问题
现在,如果我清理一些格式和所有错误,并尽可能靠近代码,结果可能如下所示:
function codeAddress(){
var answer;
answer = prompt('Have you played this game?');
if ( answer === 'no'){
confirm('This game is like Zork!');
answer = prompt('Do you need a refresher?')
if (answer === 'yes'){
confirm('All it is, is that you are given a scenario, '
+'in which you are also given actions. '
+'These actions lead you through a certain path '
+'and ending. Pick carefully!');
} else {
confirm('Lets play then!');
}
}
//beginning of game
confirm('Dazed and a little groggy, you realize that you are '
+'trapped on an island after a crash landing. \n'
+'Along with other children, you know it\'d be a best '
+'idea to go and keep everybody calm.');
answer = prompt('What are you planning to do? \n'
+'1. Stay calm and keep everybody in check, or \n'
+'2. Panic.');
if ( answer === '1'){ //stay calm
confirm('That seems like a great idea. \n'
+'You go around the island, doing your best to '
+'convince others to stay calm and keep their composure.');
answer = prompt('In the attempt to stay calm, you know that '
+'youve read about some situation like this - '
+'it was in Lord of the Flies, yes! That was it! \n'
+'With Piggy on your side as you walk the beach, '
+'you know you had to get an item to get peoples '
+'attention. \n\n'
+'What would you do? \n'
+'1. Pick up a conch laying on the beach, or \n'
+'2. Take Piggys glasses');
if ( answer === '1') {
confirm('Yeah, that was probably the best idea. \n'
+'With the conch you could blow into it and end '
+'up being able to gather attention and create '
+'a leadership.');
} else {
confirm('That works too! The sunny day lets you focus '
+'the concentrated light onto wood and create a fire.');
confirm('Soonly, as you wait, you see a helicopter from '
+'the horizon come and land. \n'
+'Oh what a miracle! Youve been saved!');
confirm('Or so you thought');
confirm('30 minutes into the flight, a dog fight occurs '
+'by your position, shooting your helicopter out '
+'of the sky, landing you onto another island, '
+'this time even farther than before.');
confirm('Game over.');
}
} else { //panic
confirm('Ahh... this isnt so good. \n'
+'Your commotion had instigated chaos and getting '
+'others to follow in your stead would be extremely '
+'challenging.');
confirm('In the choice of panicing, every attempt to gather '
+'every or even anybody to keep composure, the group '
+'of boys fail to make ends meet. \n'
+'Slowly, as each day passes, you grow weak, only '
+'for you to realize that the choices that you had '
+'made was a catalyst of failure - causing death for '
+'others, and soon enough, you.');
}
}
window.onload = function(){ codeAddress(); };
工作
其中一个变化是,用户在提示框中键入的字符串存储在变量answer
(该变量的作用域为函数codeAddress
)。
还有一些关于如何使代码更具可读性以及如何在消息/提示框中添加新行的提示
然而,需要注意的最重要的一点是,如果您查看您或我的代码,您可以看到您的故事线的嵌套和缩进将变成一个地狱
因此,你应该开始在故事情节的较小部分分解事情。
此外,您还希望有一个包含所有文本片段的数组,这样您就不需要在逻辑中键入所有文本(稍后可以翻译游戏)
希望这有帮助 您的代码中有很多错误
除了弗朗西斯科·阿丰索在回答中指出的一切之外,我想补充以下几点
最后一行:window.onload=code地址代码>
在你的小提琴中,你已经选择了“onload”选项(它已经自动包装你的javascript代码)
在else案例中,您有双块{statements here}
您在if
语句中提示了一个问题,然后再次提示相同的问题
现在,如果我清理一些格式和所有错误,并尽可能靠近代码,结果可能如下所示:
function codeAddress(){
var answer;
answer = prompt('Have you played this game?');
if ( answer === 'no'){
confirm('This game is like Zork!');
answer = prompt('Do you need a refresher?')
if (answer === 'yes'){
confirm('All it is, is that you are given a scenario, '
+'in which you are also given actions. '
+'These actions lead you through a certain path '
+'and ending. Pick carefully!');
} else {
confirm('Lets play then!');
}
}
//beginning of game
confirm('Dazed and a little groggy, you realize that you are '
+'trapped on an island after a crash landing. \n'
+'Along with other children, you know it\'d be a best '
+'idea to go and keep everybody calm.');
answer = prompt('What are you planning to do? \n'
+'1. Stay calm and keep everybody in check, or \n'
+'2. Panic.');
if ( answer === '1'){ //stay calm
confirm('That seems like a great idea. \n'
+'You go around the island, doing your best to '
+'convince others to stay calm and keep their composure.');
answer = prompt('In the attempt to stay calm, you know that '
+'youve read about some situation like this - '
+'it was in Lord of the Flies, yes! That was it! \n'
+'With Piggy on your side as you walk the beach, '
+'you know you had to get an item to get peoples '
+'attention. \n\n'
+'What would you do? \n'
+'1. Pick up a conch laying on the beach, or \n'
+'2. Take Piggys glasses');
if ( answer === '1') {
confirm('Yeah, that was probably the best idea. \n'
+'With the conch you could blow into it and end '
+'up being able to gather attention and create '
+'a leadership.');
} else {
confirm('That works too! The sunny day lets you focus '
+'the concentrated light onto wood and create a fire.');
confirm('Soonly, as you wait, you see a helicopter from '
+'the horizon come and land. \n'
+'Oh what a miracle! Youve been saved!');
confirm('Or so you thought');
confirm('30 minutes into the flight, a dog fight occurs '
+'by your position, shooting your helicopter out '
+'of the sky, landing you onto another island, '
+'this time even farther than before.');
confirm('Game over.');
}
} else { //panic
confirm('Ahh... this isnt so good. \n'
+'Your commotion had instigated chaos and getting '
+'others to follow in your stead would be extremely '
+'challenging.');
confirm('In the choice of panicing, every attempt to gather '
+'every or even anybody to keep composure, the group '
+'of boys fail to make ends meet. \n'
+'Slowly, as each day passes, you grow weak, only '
+'for you to realize that the choices that you had '
+'made was a catalyst of failure - causing death for '
+'others, and soon enough, you.');
}
}
window.onload = function(){ codeAddress(); };
工作
其中一个变化是,用户在提示框中键入的字符串存储在变量answer
(该变量的作用域为函数codeAddress
)。
还有一些关于如何使代码更具可读性以及如何在消息/提示框中添加新行的提示
然而,需要注意的最重要的一点是,如果您查看您或我的代码,您可以看到您的故事线的嵌套和缩进将变成一个地狱
因此,你应该开始在故事情节的较小部分分解事情。
此外,您还希望有一个包含所有文本片段的数组,这样您就不需要在逻辑中键入所有文本(稍后可以翻译游戏)
希望这有帮助 很抱歉,最后一部分“}window.onload=codeAddress;”被从块中删除。我还原了您原来的问题(否则所有当前的答案都将无效,因为我们需要考虑将来的读者),并添加了您的新代码/问题。很抱歉,最后一部分“}window.onload=codeAddress;”我恢复了您原来的问题(否则,所有当前的答案都将毫无用处,因为我们需要考虑未来的读者),并添加了您的新代码/问题。在某些情况下,它确实有效-但仅当if-else语句较短时有效,但对于较长的语句,解决方案是什么?因为它不会运行,除非我找到丢失分号的区域。。。另外,如果这是显而易见的,我非常抱歉,哈哈……好吧。为了修复第29行的其他错误,其中}}else{{制造了一场骚动,我确实从语句中删除了分号。但是它仍然存在于29it接缝处,就像我们在评论中有种族条件:)@FranciscoAfonso所说的是,您需要删除if语句
(你的代码中没有缺少分号,你有一些额外的。它们破坏了你的代码。)这样做:)我只找到了最后一个区域。不,if语句后面的分号仍然存在。我真的不知道如何更详细地解释。if(condition){}是正确的,但是if(condition);{}不是!在某些情况下,它确实有效-但只有当if-else语句较短时,但对于较长的语句,解决方案是什么?因为它不会运行,除非我找到丢失分号的区域…请注意。如果这是显而易见的,我非常抱歉,哈哈…好吧。为了修复第29行的其他错误,在那里}else{{制造了一场骚动,我确实从语句中删除了分号。但是它仍然存在于29it接缝处,就像我们在评论中有种族条件:)@FranciscoAfonso所说的是,您需要删除if语句
(您的代码中没有缺少分号,您还有一些额外的分号。它们破坏了您的代码)。这样做了吗:)我只找到了最后一个区域。不,if语句后面的分号仍然存在