Assignment 1 - Filling out Keywords

You hand in this assignment by showing it to the lecturer when it is completed. This assignment contains a program text where spcial Java Script words are missing. Such words are break, case, do, else, for, function, if new, return, switch, this, var, while and they are essential for programming. The below text has these keywords blacked out by "XXXXX" and you should replace each occurrence of "XXXXX" by the appropriate word. So you get some feeling on how a correct program looks like. Please do this exercise after Assignment 0.
  1. Getting Started.
    Log on your UNIX account and go into the correct directory.
            cd public_html 
    Download this page onto your computer and place it on your webpage.
            wget http://www.comp.nus.edu.sg/~gem1501/assignment01.html
            chmod 755 *.html 
    The wget command copies a file into your page.
    The chmod-command enables you to look with your browser at the downloaded assignment.
    Use an editor like pico, vi or vim to look at the file. You can type the file name as well like
            pico assignment01.html 
    so that the editor knows which file to edit and where to save the changes.

  2. Make yourself familiar with the program.
    Most of a webpage is written in Hypertext Markup Language (HTML).
    But this page contains also some parts in Java Script.
    Note that there is an other programming language with
    the name Java, but Java Script shares with it just the name.
    These parts are mixed into the html-page.
    In order to make it easier for you to learn to program
    with Java Script, you receive almost ready programs.
    So you do not need to learn html.

    Follow the link for an introduction to Java Script.

  3. Task of this exercise.
    Here some sample constructs of what should be programmed.
              // listing names of used variables, l is initialized as 8
              var x; var y; var k; var l=8;
              // for loop, k takes the values 0,1,2,3,4,5,6,7,8,9 in the loop
              for (k=0;k<10;k=k+1) 
                { document.write("Now k is "+k+" in the loop.");
              // while loop
              while (l*l<1000) { l = l+1; }
              // do loop
              do { l = l+1; } while (l*l<1000);
              // function with return-value
              // the word 'this' is used in compound return-values, but
              // this special word is omitted in this exercise.
              function f(x)
                { var y = 3*x-x*x*x; return(y); }
              // case-distinction
              switch(l)
                { case 33: case 35: case 37: case 39: case 41:
                    document.write(" Number l is odd."); break;
                  case 32: case 34: case 36: case 38: case 40:
                    document.write(" Number l is even."); break;
                  default:
                    document.write(" It is unknown what l is."); break; }
              // if statements
              if (k == 5) { document.write(" Number k is five."); }
              else { document.write(" Number k ist not five."); } 
    More information is found at the Introduction to Java Script. Note that the browser Internet Explorer helps finding the bugs. To do this you have to change the default settings.
    1. Go to the menu "Tools" and then to the last row "Internet Options".
    2. A new window "Internet Options pops up. Go to the entry "Advanced" in the list of subwindows on the top.
    3. Check the box "Display a notification about every script error" (sixth box under the entry "Browsing").
    4. Press "OK" in the lowest row to finish.
    Having done this, you should on one hand edit the file with the text of this page and on the other hand press the "refresh" or "reloading" button of the browser to see the effect of what you are doing.
    Note that you should view the details of the announced Java Script errors to get the line numbers.

  4. What the correct program does.
    The corrected program computes function f,g,h for numbers from 1 to 9. After that it computes the first number i where h(i)>=1000 and counts down j to 0. It outputs f(i), g(i), h(i) for the just computed i. Then i and j take new randomly chosen values, are compared and at the end the word for the numerical value of i is output.

Java Script Starts Here.


Java Script Ends Here.