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.
- Getting Started.
Log on your UNIX account and go into the correct directory.cd public_htmlDownload this page onto your computer and place it on your webpage.
wget http://www.comp.nus.edu.sg/~gem1501/assignment01.html chmod 755 *.htmlThe 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 orvim to look at the file. You can type the file name as well likepico assignment01.htmlso that the editor knows which file to edit and where to save the changes.
- 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.
- 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.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.
- Go to the menu "Tools" and then to the last row "Internet Options".
- A new window "Internet Options pops up. Go to the entry "Advanced" in the list of subwindows on the top.
- Check the box "Display a notification about every script error" (sixth box under the entry "Browsing").
- Press "OK" in the lowest row to finish.
Note that you should view the details of the announced Java Script errors to get the line numbers.
- 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.