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
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation JavaScript Personal Project (20 Questions) Problem

    I have decided to use my recently-obtained knowledge of the Javascript mini-programming language to (at least start to) create a 20 Questions app. I have lots of time to do this, so please don't correct inefficiency problems, just things that will cause it to not work. I am creating this on labs.codeacademy.com, and it is spitting out the error: "Missing ")" in the last line." although I don't really understand why. Here's my code:

    Code:
    alert("Hello, I am Thomas, the 20 Questions Robot. Press OK to continue.");
    var name = prompt("I've introduced myself. Now it's your turn. What's your name?");
    alert("Awesome! Nice to meet you, " + name);
    var age = prompt("Which year were you born in, " + name + "?");
    if (age > 2000) {
    alert("Age did not meet recommendation. If you wish to continue anyway, press OK. Otherwise, quit your browser.") }
    else { alert("Age accepted! Press OK to continue.") };
    var understand = prompt("Each question must be answered with a yes / no represented by Y or N. Do you understand this?");
    if (understand === "Y") {
    alert("Good. Enough of this pre-game nonsense, let's start! Click OK to BEGIN 20 Questions!") }
    else { alert("Incorrect answer, " name + "Do not make this mistake during the game! Press OK to continue!") }
    Please correct my error(s) and briefly explain why it needs to be changed / what is wrong... Thanks in advance

    -isaacnhh98

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,983
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Use your error console (F12 key) to reveal errors.

    else { alert("Incorrect answer, " + name + "Do not make this mistake during the game! Press OK to continue!") }

    alerts are regarded as obsolete and should be used only for testing purposes. Use DOM methods to display a message to the user.
    prompts are also obsolete - again, use textboxes to obtain user input data.

    If you do insist on using prompts, you need to add "" to your prompts as otherwise they show undefined. E.g:-
    var name = prompt("I've introduced myself. Now it's your turn. What's your name?","");
    But you should avoid giving names or id's to your variables/functions/arguments/forms words which are HTML/JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'test' or 'text' or 'checked' or 'click' or 'href' or 'closed' or 'go' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.


    Your scripts allow silly answers such as name or age 45$7%?, or nothing at all (null). The reality is that 80% of scripts which require some sort of user input consist of validation.
    Last edited by Philip M; 06-29-2013 at 08:12 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.


  •  

    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
    •