



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Instructions for a JavaScript practical exercise aimed at helping students identify logical errors in their code. It includes creating a new HTML file and a separate JavaScript file, using debugging techniques such as console.log and the JavaScript debugger in Google Chrome, and working with arrays and functions. The exercise also touches upon the use of objects in JavaScript.
What you will learn
Typology: Lecture notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!
https://cgi.csc.liv.ac.uk/~ullrich/COMP284/notes/practical07.pdf
and you might proceed more quickly if you cut-and-paste code from that PDF file. Note that a cut-and-paste operation may introduce extra spaces into your code. It is important that those are removed and that your code exactly matches that shown in this worksheet.
a. Start a new file js07A.html in your public_html directory with the following content:
Then create a separate file js07A.js in your public_html directory with the following content: function random() { var mode = Math.floor((Math.random()4)+1) / Add debugging code here / switch (mode) { case 1: randvar = Math.round(Math.random()100); break case 2: randvar = String(Math.random()) break case 3: randvar = Math.random()/(Math.random()+1) break default: randvar = Boolean(Math.random()+0.5) break } }
b. Save the files and make sure that their access rights are set correctly using the command
chmod u+r,og-rwx $HOME/public_html/js07A.*
(Note: No space after the comma!) You will only have to do so once. File permissions should not change while you continue to edit the file. c. Open a web browser and access the URL https://student.csc.liv.ac.uk/∼
where
d. Suppose that you are not sure what branch of the switch-statement is executed each time the function random() is called and you would like to know what the values of the variables i and mode in each iteration of the for-loop are to figure that out. To get this information you can add the following debugging code at the point inside the definition of random() indicated in the code. console.log("i =",i," mode =",mode)
a. Open a text editor and enter the following JavaScript code:
b. Save the code to a file named js07B.html in $HOME/public_html/. Make sure that the access rights for the file are set correctly. c. Open js07B.html in a web browser. Right now the scripts do not do much and the output you should see is simply The sum of cells around [1,1] is undefined
You can also have a look at the console where you should see that a two-dimensional array has been created and filled with random natural numbers between 0 and 100. d. Complete the function showArray with code that displays the content of the two- dimensional array ar as a two-dimensional HTML table. Each HTML table cell should contain the co-ordinates of the cell and value in ar stored at those co-ordinates. Here is an example of what the HTML table should look like: [0][0] 34 [0][1] 9 [0][2] 40 [1][0] 39 [1][1] 18 [1][2] 74 [2][0] 38 [2][1] 77 [2][2] 1 e. Next, we want to define the function sumAround(). The function takes an array ar and natural numbers x and y as arguments and should return the sum of the values stored in ar at co-ordinates ‘surrounding’ (but not including) [y][x]. For [y][x] = [1][1], the cells ‘surrounding’ [1][1] are ar[0][0], ar[0][1], ar[0][2], ar[1][0], ar[1][2], ar[2][0], ar[2][1], ar[2][2]. The return value of sumAround(ar,1,1) for the array ar above should be 312. For [y][x] = (2, 2) the cells ‘surrounding’ [2][2] are just [1][1], [1][2], [2][1]. The return value of sumAround(ar,2,2) for the array ar above should be 169. f. Finally, we develop the function maxAround(). The function takes an array ar and natural numbers x and y as arguments and should return the maxium value stored in ar in cells ‘surrounding’ (but not including) [y][x]. For the array ar above and co-ordinates [y][x] = [0][1], maxAround(ar,1,0) returns 74. For the array ar above and co-ordinates [y][x] = [2][0], maxAround(ar,0,2) returns 77.
a. Open a text editor and enter the following JavaScript code: