Part 1 - Review Quiz

  1. Find and describe three syntactical problems in the following piece of code, and then re-write it correctly:

    // this is a short JavaScript program  
    alert("Hello there!");
    alert("Welcome to JavaScript!);
    alert("Have a nice day!";

  2. Answer: First, the second alert needs an end quotation mark after the string. It should appear as follows:

    alert("Welcome to JavaScript!");

    Second, the final alert needs an end parenthesis to complete the alert function.

    alert("Have a nice day!");

    Finally, it's not clear whether this JavaScript is contained within a .js file or an HTML file. If it is in a .js file, no additional markup is needed. If it's in an HTML file, it will need to have a script tag surrounding it:

    <script type="text/javascript" language="JavaScript">
        // this is a short JavaScript program
        alert("Hello there!");
        alert("Welcome to JavaScript!");
        alert("Have a nice day!");
    </script>
  3. Write the number identifying the correct answer: 5. They are all methods.

    Which of the following does not refer to functions or methods?

    1. alert();
    2. confirm();
    3. prompt();
    4. toUpperCase();
    5. None of the above

  4. Answer True or False for each of the following statements:

    1. It doesn't matter whether you use upper or lower case in JavaScript. False.
    2. As long as you open strings with quotes, you don't have to close them; it's implied. False.
    3. Methods are commands that do things. True? This is a bit vague.
    4. Using white space helps to make programs more readable. True.

  5. The following code produces errors when run in a browser. Clean it up so it works! Save it as part1d.html and place it in the wts/asst2 you created in Exercise 2.

    All of the errors are ones that you should be able to recognize easily; you don't have to worry about constructions you don't understand yet. Some of the errors are in the HTML, others in the script itself. In total, there are three errors to fix before the script will run correctly.

    Actually, the code will run fine with the addition of just two quotation marks, as marked in bold below. Click here to run the script.

    <html>
    <head>
        <title>Poetry Reading</title>
    </head>
    <body>
        <script type="text/javascript" language="JavaScript">
        // get the user's response and store it as "goOn"
    
        var goOn;
    
        goOn = confirm("Press OK if you want to read poetry");
    
        /* if they answered "OK", give them poetry
        if they answered "Cancel", no poetry */
    
        if (goOn == true)
           {
              alert("I never saw a purple cow");
              alert("I never hope to see one");
              alert("But I can tell you anyhow");
              alert("I'd rather see than be one");
           }
        else
           {
              alert("OK, no poetry today."); 
           }
    
    </script>
    </body>
    </html>