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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help! A grade 9 student has an error in his code!

    and his script has an error. (I teach Computer Technology, he is working on learning to code in his 20% time.... )

    For the life of me, I cannot see what is wrong with this:

    // Check if the user is ready to play!
    confirm("Are you ready to play?");
    var age = prompt("What's your age?");
    if (age < 18){
    console.log('You are able to play but i take no responsibilty.');
    } else {
    console.log("Have a great game!");
    }
    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 = prompt("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.");
    }


    But the error is after

    if (userAnswer === yes) {

    Help?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    yes is seen as a JavaScript variable.

    Since you never defined that variable, *KABLOOEY*.

    I'm sure what you meant to code was
    Code:
    if ( userAnswer === "yes" ) {
    But that's not the best idea. What happens if the user types in "YES" or "Yes" or just "Y"?

    A better solution:
    Code:
    if ( userAnswer.toUpperCase().charAt(0) === "Y" ) {
    And you will pardon me for not believing the bit about you teaching Computer Technology.
    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.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    By the by, console.log() will not work in all browsers.

    Time to learn better techniques.
    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! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    On, and this line is pointless:
    Code:
    confirm("Are you ready to play?");
    The game proceeds no matter what the user answers.
    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.


  •  

    Posting Permissions

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