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

    My java programming wont work

    I am new to java. can someone tell me whats wrong

    confirm("Are You Ready to Play")
    age = prompt("What's your age?")
    if(age < 13)
    {
    console.log("I take no responsibility")
    }
    else
    {
    console.log("Have Funnnn!!!");
    }
    console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
    console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
    var userAnswer = prompt("Do you want to race Bieber on stage?");
    if(prompt = yes)
    {
    console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
    }
    else
    {
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,142
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Your code is Javascript. Be aware that Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!


    Have you tried using your error console (F12 key)?

    Suggest the following alterations:-

    var ready = confirm("Are You Ready to Play",""); // all variables should be defined with the var keyword. (But whatever the answer the script proceeds)

    var age = prompt("What's your age?", "") // Eliminate ugly "undefined" in prompt.
    (But what if user responds "Mickey Mouse" or whatever (not a number)?)

    var userAnswer = prompt("Do you want to race Bieber on stage?","");
    if ( userAnswer == "yes" ) // = is assignment, == is comparison. "yes" must be in quotes as it is a literal string. if (userAnswer == yes) compares the answer with the value of a variable named yes (which does not exist).
    (but what if user responds Yes or YES or Y?)


    It is strongly recommended that you place the opening brace following the function, if, else, for, while, do, switch, and try statements on the same line and not on the following line. Apart from that every Javascript statement should be followed by a semi-colon (;). It is quite possible to disregard this advice, but if you do one day it will rise up and bite you in the undercarriage.

    Be aware also that prompts and alerts are long obsolete, and should be used only for testing purposes. Not for obtaining responses from and/or displaying messages to your users.
    Last edited by Philip M; 05-14-2014 at 07:59 PM.

    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.


  •  

    Posting Permissions

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