Mini-Lab 1: Getting Started with JavaScript


Introduction

In this mini-lab you will modify JavaScript functions to experiment with strings, numbers, the + operator, and calling functions.

If your code isn't working as expected, try opening up Firefox's error console to check for (potentially) helpful error messages. If you get stuck, don't hesitate to ask the instructor or a teaching assistant for help.


Experimenting with Numbers and Strings:

  1. Copy the Getting Started with JavaScript web page to your COMP 105 workspace. You can do this in either of two ways:
  2. View the page source. Scroll down to the bottom third of the page, find where the BODY tag begins, and make sure you understand the HTML code there.
  3. Find the INPUT tag that creates a button. What function will get called when the user clicks on the button? Find that function up in the HEAD section, near the top of the page. (What you just found in the HEAD is called the function definition, whereas the reference to the function in the BODY is called a function call.)
  4. There are many statements in the function definition; all but one are "commented out" (enclosed in comments so they won't run). Based on your reading of the one statement that is not commented out, what do you think the program will do if you click on the "Run Experiment" button? Click on it to see if you were right.
  5. Uncomment the second alert statement in the function by removing the // symbols at the beginning of the line. (There will still be a comment to the right of the statement, at the end of the line.) What do you think the function will do now? Save the file and preview or reload the page, then click on the "Run Experiment" button to test your understanding. Update the comment at the end of the line to show what the output was.
  6. Now comment out both of the first two statements.
  7. In the first two statements, you displayed a string and a number. The next five statements will let you experiment with what the + operator does inside and outside strings, and when given two numbers, two strings, or a number and a string.

    For each of these statements, indicate what you think the output will be in the comment at the end of the line. Then uncomment the statements, either one-by-one or all at once, save and re-preview/reload the file, and click "Run Experiment", comparing the actual output values with the values you expected. For each value that is different from what you expected, look at it again and see if you can see why the output was the way it was. If not, ask a neighbor, instructor, or TA to explain it to you. Update the end-of-line comments if necessary, to reflect the actual output.


Publish to your web site:

  1. Make sure that you have updated the "Author:" and "With assistance from:" comments at the top of your source file.
  2. Edit the course "home page" you created in Lab 1 and add a link to your new mini-lab page. Since this is just the first of many links you will be adding, you may want to create a heading and a list; for example,

    COMP 105 Assignments

    • Link to Mini-Lab 1: Numbers and Strings

    In the link, refer to your new page with a relative pathname which is just the name of the file (e.g., <a href="MiniLab1.html">). This tells the browser that the file to look for is in the same directory or folder as the current file (your main COMP 105 web page, in this case). You do not want to give a full or absolute pathname, like <a href="file:///Desktop/MiniLab1.html">, because the location of your file on the comp105.cs.kzoo.edu server will not be the same as on your own laptop or classroom computer.

    If you worked in a team, each member of your group should do this, so each of you has a link to the new mini-lab page. After the link, list your teammates in parentheses; for example, "Numbers and Strings ML 1 (with Rey and Finn)".

  3. Upload both your modified COMP 105 web page and your page for this Mini-Lab to the comp105.cs.kzoo.edu server. Test that the link to the Mini-Lab works on the server by clicking on it from your home page there. (Make sure that your browser is looking at the page on comp105.cs.kzoo.edu, not your local version.)