Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    If/Else statements not executing!

    When I run the code all that runs are the prompts. Both true and false conditions will not execute.

    <html>
    <head>
    <title>My Script</title>
    <script type="text/javascript">

    confirm("I'm ready to play!");

    var age=prompt("What's your age?");
    if(age > = 18 ){
    console.log ("You may play, but we take no resposibility");
    }else{
    console.log ("play on");}
    console.log("Snow White and Batman were hanging out at the bus stop, waiting to go to the shops. There was a sale on and both needed some new threads. You've never really liked Batman. You walk up to him.");
    console.log("Batman glares at you.");
    var userAnswer= confirm("Are you feeling lucky, punk?");

    if(userAnswer==="yes"){
    console.log("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");
    }else{
    console.log("You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman.");
    }
    var feedback=prompt("Please rate this game 1-10");
    if (feedback < = 8){
    console.log("This is just the beginning of my game empire. Stay tuned for more!");
    }else{
    console.log("I slaved away at this game and you gave me that score?! The nerve! Just you wait!");
    }

    </script>
    </head>
    <body>
    </body>
    </html>

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Have you opened the console to check what error messages are being written there if your log messages are not being written there?
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,495 Times in 4,459 Posts
    3 to 1 odds he has no idea where the console *IS*.

    jenbaby: In any case, the operators >= and <= can *NOT* have a space before the = sign.
    Last edited by Old Pedant; 02-19-2013 at 09:11 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    And please read the form guidelines about the use of code tags. Putting your message in big red characters turns people off (it did me - I just moved on!) and is considered boorish.

    if(userAnswer==="yes"){

    What if the user types Yes, y or Y?

    But I see that your userAnswer comes from a confirm box so is true or false, not yes or no.

    if(age > = 18 ){

    What is the user answers "Mickey Mouse"?

    var feedback=prompt("Please rate this game 1-10");

    What if the user enters -10 (my evaluation, I am afraid! )
    Last edited by Philip M; 02-20-2013 at 09:47 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Dec 2012
    Location
    Ingerlund
    Posts
    66
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Hello good sir. For everyone else on here, I apologize profusely if I give this guy a wrong answer. I just feel I have to contribute something instead of just asking questions!

    Firstly, this game seems to be the only game I've ever seen that issues a warning only if you are over 18, and secondly the whole problem with the code is simply as Old Pedant already said, the spaces between the > and =. Change that and it should work.


    EDIT: I'm assuming this is for some sort of learning exercise so I didn't attempt to try and 'improve' your code.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by Entity_ View Post
    Firstly, this game seems to be the only game I've ever seen that issues a warning only if you are over 18
    Well, you would have to be pretty juvenile to see anything interesting in this "game".

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Dec 2012
    Location
    Ingerlund
    Posts
    66
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Philip M View Post
    Well, you would have to be pretty juvenile to see anything interesting in this "game".
    GOTY dude, have faith. I'm submitting this to kickstarter.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •