Javascript Adventure based game - HELP!!!

Discussion in 'JavaScript' started by Oppi, Jul 13, 2016.

  1. #1
    I am new at Javascript and love learning more about it. I am having difficulties with the if/else statement when the user gives particular answers. I can get the read out I want but then at the end it continues to prompt and alert when instead I would like it to skip to the rating of the game. If you answer the prompts: Age dont matter , "Yes", "No or Yes" take the money. it still shows the prompts if you answered "No" you would not help the girl. If you answer "No" right away it doesn't include the Yes scripts. I know it has to be something with the placement of the syntax. The code is below.

    =============================================

    confirm("PokemonGo: Adventure \n Click OK to play")
    
    var age = prompt("What's your age");
    
    if(age < 13)
    {
      alert("I take no responsibility for you playing. This game contains inappropriate material for children.\n Please leave now !!!");
    }
    
    else (age > 13)
      confirm("Have Fun. Play Hard");
      confirm("You walk to a nearby park to catch a rare pokemon that you seen in your area.\n When you get there you see that you are only 1 footprint away from the Pokemon you want.")
      confirm("You look to your left and see that a very small girl, maybe 8 years old is playing PokemonGo also.\n You notice in the bottom right corner of her screen she is tracking the same Pokemon");
      confirm("She looks up and notices you and says, \n 'Can I have the rare Pokemon in this area?");
    
    var userAnswer = prompt("Do you help her capture the Pokemon?");
    
    if (userAnswer.length > 2)
    {
      confirm("You turn off your PokemonGo app. You and the little girl, which you have now learned is named Jese, spend another 5 minutes walking around before you find a Mew. Jese pulls out a $100 bill and tries to give it to you for helping her.\n ");
    
      var userAnswer2 = prompt("Do you take the $100?");
    if (userAnswer2.length > 2)
      confirm("Congratulations you just took $100 bucks from a little girl. Are you happy with yourself? (rhetorical) "); // right after here is where I want it to skip to Feedback instead of continuing the script
    
    else (userAnswer2.length < 3)
      prompt("YOU WIN!!! You are a good person all around. Pat yourself on the back.");
    }// and right after here is where I want it to skip to Feedback instead of continuing the script
    
    else (userAnswer.length < 3 )
      alert("You take the little girls phone and turn it off, walk over to the nearest tree and put it just out of her reach. She starts crying as she frantically tries to get the phone back. You leave her to her tears and begin to search for that rare Pokemon. It takes about 2 minutes before you find a Mew. You attempt to capture it like the Master you are and add it to your Pokedex. ");
    
    
    
    prompt("Do you throw a curve ball to try and win extra xp during this capture?")
    
      alert("Doesn't Matter....You Lose!!! \n Karma catches up to you. You get blindsided by the little girl's dad.\n He begins beating on you with a Louisville slugger.\n You drop your phone and the little girl picks it up and chucks it in the nearby lake. You end up in intensive care \n Life Lesson: Don't be a dick, PokemonGo is just a game." );
    
    
    
      var feedback = prompt("Rate my game out of 10")
    
      if(feedback > 2)
    {
      console.log("Thank you! We should catch Pokemon together sometime!")
    }
      else { ("I'll keep practicing my coding.") }
    Code (markup):

     
    Last edited by a moderator: Jul 13, 2016
    Oppi, Jul 13, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    The syntax is correct, but the placement of the code is a bit wonky. First off, if you need to "break" the script based on specific answers, you need to nest the if /else conditions, and probably throw in a few else if as well. Also, your choice of using less than and long than with length is REALLY not a good choice. Remember, users are stupid, or do things you wouldn't expect, and if I write "ok" for "yes" the wrong clause will trigger. It's way better to code something specific instead of using confirm / prompt so you can control what the user chooses. It should be a simple choice of Yes / No when that's appropriate, "continue" or "back" for other choices and so on, so the user can navigate and maybe try out the other possible choices.

    Have a look here: http://stackoverflow.com/questions/9334636/javascript-yes-no-alert and see if you can find something there (the last entry with code)
     
    PoPSiCLe, Jul 13, 2016 IP
  3. fehtrar

    fehtrar Active Member

    Messages:
    11
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    80
    #3
    Didn't read all your code but just a few and I see you don't get how the confirm function work and how to know if user accept or not.
    Take a look here http://www.w3schools.com/jsref/met_win_confirm.asp .

    Confirm function return a boolean so just use:

    
    var isAgirl = confirm("Are you a girl?");
    if (isAgirl === true) {
      // girl in the mirror
    }
    
    Code (markup):
     
    fehtrar, Jul 13, 2016 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Or, just use
    
    var confirm = confirm("Are you a girl?");
    if (confirm) { //this will work just fine, no need to add a typecast true
      //yes-condition goes here
    }
    
    Code (markup):
    I still would say that for the good of the player, and for a useful interaction, you need to create a different approach, not using the alert/confirm popups, but creating your own code which will display things in a nicer way.
     
    PoPSiCLe, Jul 14, 2016 IP
  5. Oppi

    Oppi Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #5
    Thanks for the tips and tricks guys. I initially was using Yes or No values but was concerned about case sensitivity. Does "NO" register the same as "no" or "No"? I am using this game for practical learning purposes only. Although, it is a dream of mine to create a fully functioning game. I am very curious as to how to create UIs but haven't gotten that far on CodeAcademy quite yet. I hope they cover it in the free version. IF not Ill be looking at Youtube and forums like this one. I am learning how to create and call functions right now.. so I am very "wet behind the ears".
     
    Oppi, Jul 14, 2016 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    No, "NO","No" or "no" doesn't necessarily mean the same thing. Well, it means the same thing, but it doesn't necessarily register as the same thing. Hence why you should refrain from giving a user the ability to write an answer, since it will always lead to things you haven't considered, and will either give an unsatisfactory result, or trigger some kind of error. You could of course throw an error if the answer doesn't match "yes" or "no", but that isn't really user friendly. Better to give the user specific, selectable answers.

    As for the difference in case when it comes to answers: it's usually always smart to force the answer from the user into either all uppercase or all lowercase for comparisons. That way, even if the user types "NO" and you expect "no", you force the user input to be lowercase, so that you will actually compare "no" to "no" and get the expected answer. That, however, is a small thing to cater for. What if the user writes "nope", or "not", or "don't" or anything else that can be construed as "no"? It's difficult to consider all the possible things a user can write in such inputs, so therefore, limit the choices.
     
    PoPSiCLe, Jul 14, 2016 IP
  7. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #7
    JavaScript is case sensitive. "NO" and "no" isn't the same thing.
     
    ketting00, Jul 15, 2016 IP