简体   繁体   English

Javascript新手-使用JSfiddle,缺少分号,但我不完全了解

[英]Newbie in Javascript - Using JSfiddle, missing semicolon but I don't fully understand where

Alright, so I'm creating a little amateur game using Javascript by using the confirm function, prompt, and if - else statements. 好吧,所以我要通过使用Confirm函数,提示符和if-else语句使用Javascript创建一个业余游戏。 However, in my code, which you can find here: http://jsfiddle.net/ShiiFtyyMendoza/yFzxj/15/ 但是,在我的代码中,您可以在这里找到: http : //jsfiddle.net/ShiiFtyyMendoza/yFzxj/15/

 function codeAddress() {
     if (prompt('Have you played this game?') === 'no') {
         confirm('This game is like Zork!');
         prompt('Do you need a refresher?');
         if (prompt('Do you need a refresher?') === '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. Along with other children, you know itd be a best idea to go and keep everybody calm.');
     //stay calm.
     prompt('What are you planning to do? 1. Stay calm and keep everybody in check. or 2. Panic.');
     if (prompt('What are you planning to do? 1.Stay calm and keep everybody in check. or 2. Pacnic') === '1.'); { //this ends after line 19
         confirm('That seems like a great idea. You go around the island, doing your best to convince others to stay calm and keep their composure.');
         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! With Piggy on your side as you walk the beach, you know you had to get an item to get peoples attention. What would you do? 1. Pick up a conch laying on the beach, or 2. Take Piggys glasses');
         if (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! With Piggy on your side as you walk the beach, you know you had to get an item to get peoples attention. What would you do? 1. Pick up a conch laying on the beach, or 2. Take Piggys glasses') === '1.'); {
             confirm('Yeah, that was probably the best idea. 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. 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 {
         {
             confirm('Ahh... this isnt so good. 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. 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 = codeAddress;

I have an error in line 21 and 29 because of a missing semicolon. 由于缺少分号,我在第21和29行出现错误。 What am I doing wrong? 我究竟做错了什么? I don't really know the complicity of JS, so I'm quite lost! 我不太了解JS的同谋性,所以我很迷路! Any help is appreciated! 任何帮助表示赞赏!


UPDATE 更新
Based on GitaarLAB's answer, I created the following code: http://jsfiddle.net/ShiiFtyyMendoza/yFzxj/24/ 根据GitaarLAB的回答,我创建了以下代码: http : //jsfiddle.net/ShiiFtyyMendoza/yFzxj/24/

 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') { { { { //pick conch 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.'); answer = prompt('With a tool to help gather attention,' + ' itd be a shame if you didnt put it to good use. \\n' + ' Would you 1. try to preserve civilization on the island? \\n' + ' or 2. go primitive, in an attempt to escape quickly'); if (answer === '1') { //establish leadership - to follower else confirm('With the help of others,' + ' you were able to create multiple shelters' + '. By doing so, you were able to increase your chance of surviving.') answer = prompt('With the comfort of having shelter, you are led into a disagreement with Jack.' + ' He, himself creates a separate group, weakining the once joint operation.' + 'you have a choice to fight back, in an attempt to be able to all go home together as one. \\n' + 'What is your decision? 1. Stay calm and sleep in your newly made shelter \\n' + 'or \\n' + '2. Plan your escape with the others.'); if (answer === '1') { //sleep calmly confirm('After your little disagreement, you decided to stay calm, and sleep instead of rising up and fighting.' + ' But as soon as you come out of your shelter, you realize that something is oh so very wrong.' + ' Nobody is here! Everybody else had been eloquently convinced by Jack to leave your intention for his.' + ' That worthless hunting group. He put the human primitive nature in front of priorities!'); answer = prompt('You do have a choice though. /n' + '1. Would you go lone wolf and work hard to save yourself? \\n' + '2. or would you instead look for them? In the hopes of joining their league.'); if (answer === '2') //look for them { { confirm('With the decision to walk around and try to find the group,' + ' you hear a chant - the hunting chant.' + ' They were close by...' + ' As you closed up, however, you were attacked by Roger and Jack.' + ' How weird. You dont remember any reason for hostility, until Sam and Eric came by.' + ' Hey! OVER HERE! said Sam and Eric \\n' + ' As you came closer, their voices became fainter' + ' Run, they said. That was all they said.'); confirm('You didnt think so at first, but the twins were right.' + ' As soon as you started to run, you were spotted by Jack.' + ' With a sadistic sense, he lit the forest they had all once occupied, on fire.'); confirm('Closer and closer, you ran towards the shore,' + ' but it was a trap!' + ' There was no way out, they had lit the forest on fire to drag you out.'); confirm('Life seems to flash before you, until you realize that a ship started to aproach.' + ' Out came the navy officer, and you were finally saved.'); confirm('Good game!'); } } else { //fire signal - from if followers are gone confirm('You try to go lone wolf,' + ' in order to, you had created different signals to be seen.' + ' One, was a drawn message on the shore.'); confirm('Another attempt was to set fire on to the forest.'); confirm('This would have worked, however fights had not occured by your position, and you were never discovered.'); confirm('Without any materials to create signals, and the death of many animals and other sources of food,' + ' you slowly perished.'); confirm('Game over'); } } } else { { //raft from followers, if from establish leadership confirm('You in the end decide to leave it all, and to do so, you desperately thought of an option to use as an escape.') confirm('With the others boys, you decided a plan.') confirm('Going through the island, looking for stable pieces of wood, you decided to build a raft.') confirm('What a great idea! Youll be able to go home!') confirm('At least you hoped so.') confirm('That same day, as you hopelessly rowed away from the island, a storm hits.') confirm('With your lack of ability to swim for long periods of time, you fatigue and slowly drown to your death.') confirm('It would have probably been better to stay with the other boys at the island.') confirm('Game over.') } } } else { //glasses 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(); }; 

What do you think? 你怎么看?

For example: 例如:

if (prompt('What are you planning to do? 1.Stay calm and keep everybody in check. or 2. Pacnic') === '1.');

Simplified version: 简化版:

if (condition);

If statements don't have semicolon at the end. 如果语句末尾没有分号。 PS: You make the same mistake a few lines later. PS:几行之后,您犯了同样的错误。

In your code there are a lot of errors. 在您的代码中有很多错误。

Apart from everything Francisco Afonso pointed out in his answer, I'd like to add the following. 除了弗朗西斯科·阿方索(Francisco Afonso)在其回答中指出的所有内容外,我还要补充以下内容。

Your last line: window.onload = codeAddress; 最后一行: window.onload = codeAddress;
In your fiddle you already had the 'onload' option selected (which already automatically wraps your javascript code). 在您的提琴中,您已经选择了“ onload”选项(该选项已经自动包装了您的JavaScript代码)。

You had dual blocks { statements here } in your else-cases. 在其他情况下,您有双块{ statements here }

You prompt ed a question, then prompted the same question again in your if statement. prompt了一个问题,然后在if语句中再次提示了相同的问题。

Now if I clean up some formatting and all errors, trying to stay as close to your code as possible, the result might look like this: 现在,如果我清理了一些格式设置和所有错误,并尝试尽可能地靠近您的代码,结果可能如下所示:

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(); };

Working JSFiddle here . 在这里工作的JSFiddle

One of the changes are that the string that a user types in the prompt-box is stored in the variable answer (which is locally scoped to the function codeAddress ). 更改之一是用户在提示框中键入的字符串存储在变量answer (该变量在本地范围内作用于function codeAddress )。
There are also some pointers in how you could make your code more readable and how to do new-lines in your message/prompt boxes. 关于如何使代码更具可读性以及如何在消息/提示框中进行换行,还有一些指示。

The most important thing to note however is that if you look at your or my code, you can see that nesting and indenting of your story-line will become a hell! 但是,最重要的一点是,如果您查看自己的代码或我的代码,就会发现故事情节的嵌套和缩进将变成地狱!
So you should start to break things up in smaller parts of the story-line. 因此,您应该在故事情节的较小部分开始分解。
Also you'd want to have a array containing all pieces of text, so you don't need to type all the text inside your logic (and can translate your game later on). 另外,您还希望有一个包含所有文本的数组,因此您无需在逻辑内键入所有文本(以后可以翻译您的游戏)。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM