Deliverables:
To complete this assignment you must --
1.) Write the HTML and JavaScript page described below and demonstrate to
the lab instructor that it operates correctly.
2.) Complete the lab during the time provided or email your results to your lab instructor.
3.) Demonstrate youir program(s) to your lab instructor.
Introduction: The goal of this assignment is to give you experience working with arrays, IF-THEN-ELSE blocks, and LOOPS. Remember, arrays allow one variable name to refer to multiple memory locations. IF-THEN-ELSE blocks let you choose what parts of your code execute based on input data. LOOPS allow you to repeat sections of code. These three things exist in some Fform in all programming languages you are likely to encounter.
Procedures:
PART 1:
Section | Pseudocode | Comments | |
---|---|---|---|
INPUT: | var list[10]; | This represents a list of ten numbers. | |
OTHER VARIABLES: | var sum; var i; | ||
INITIALIZATION: | FOR i = 0 to 9 DO list[i] = random integer between 0 and 100; END DO sum = 0; |
This fills the array with ten random numbers from 0 - 100. Notice how we can use pseudo-code to avoid headache of having to build a random number generator. Most languages have predefined ones, and I'll demonstrate how to use the Javascript one in the lab. | |
COMPUTATION: | FOR i = 0 to 9 DO sum = sum + list[i]; ENDDO |
This loop simply adds each list element to the sum one by one. | |
OUTPUT: | Display sum using document.write() or something similar. |
Remember to format your results in a sensible way. |
Compare numbers from two separate lists.
Section | Pseudocode | Comments | |
---|---|---|---|
INPUT: | var list1[10], list2[10]; | This represents two lists of ten numbers each. | |
OTHER VARIABLES: | var i; | We only need the loop variable (also called a counting variable. | |
INITIALIZATION: | FOR i = 0 to 9 DO list1[i] = random number between 0 - 100; list2[i] = random number between 0 - 100; ENDDO |
i = 0 to 10 not inclusive of 10. | |
COMPUTATION: | FOR i = 0 to 9 DO IF ( list1[i] > list2[i]) THEN print(list1[i] + "is greater than" + list2[i]); ELSE print(list1[i] + "is less than" + list2[i]); ENDIF ENDDO |
This simply compares two corresponding numbers from each list and prints out which one is bigger. The print() statements mean any display function, such as document.write(). | |
OUTPUT: | No output required since we have printed out the results in the loop body. |