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 special JavaScript 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 keyword. So you get some feeling on how a correct program looks like. Please do this exercise after Assignment 0.

  1. Getting Started

    Please refer to this page for information on how to work on your assignment.

  2. Make yourself familiar with the program

    Most of a web page is written in Hypertext Markup Language (HTML). But this page contains also some parts in JavaScript. Note that there is an other programming language with the name Java, but JavaScript 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 JavaScript, you receive almost ready programs. So you do not need to learn HTML.

    Follow the link for an introduction to JavaScript.

  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 is not five.");
    }
    

    More information is found at the Introduction to JavaScript. 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 titled Internet Options pops up. Go to the Advanced entry in the list of subwindows on the top.
    3. Check the Display a notification about every script error box (sixth box under the Browsing entry).
    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 JavaScript errors to get the line numbers.

  4. What the correct program does

    The corrected program computes the f(),g() and h() functions for numbers from 1 to 9. After that it computes the first number i such that h(i) >= 1000 and counts down j to 0. It outputs f(i), g(i) and 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.

JavaScript Starts Here.
JavaScript Ends Here.