


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
Javascript coding exercises for beginners. The first exercise involves creating a 'hello world' webpage with dynamic user name display using javascript. The second exercise covers equality comparisons and prints the results of comparisons between different data types using an html table.
Typology: Lecture notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



< html > < head > < title > Hello World
< body >
Our first JavaScript script
< script type =" text / javascript " > user = "{ your name }"; document. writeln ("Hello " + user + " < br / >\ nHello World!
")This is the rest of the page
< noscript > JavaScript not supported or enabledReplace {your name} with your own name.
b. Save the code to a file named jsDemo1.html in $HOME/public_html/.
c. Open a terminal, go to the directory in which the file has been stored and make the file readable for everyone by using
chmod a+r $HOME/public_html/jsDemo1.html
You will only have to do so once. File permissions should not change while you con- tinue to edit the file.
d. Now open a web browser and access the URL
http://www.csc.liv.ac.uk/∼{user}/jsDemo1.html where {user} should be replaced by your departmental user name. Make sure that the web page you are shown corresponds to the HTML code you have seen in 1c. above. Hint: Your web browser can shown you the HTML source of a web page. e. If already at this point or in one of the later exercises the web browser does not show the expected result, you will have to debug your code. For that you will need access to some diagnostic output for the JavaScript code that you have written. In a lot of web browsers there is some sort of ‘error console’. For example, in Firefox the error console can be found in the ‘Tools’ menu. The shortcut CTRL+SHIFT+J will also open the error console. Figure out how to access the error console in your web browser and open it. f. To see whether how the error console works let us introduce an error into our JavaScript code. Before the tag insert document. writeln (" This is an undeclared variable : " + userAge + " < br / >")
into jsDemo1.html. Save the file again and refresh the page in your web browser. In the error console you should now see an error message indicating that userAge has not been defined/declared.
g. We should correct the error by adding a declaration for userAge.
First, try the following: Add var userAge
to jsDemo1.html before the tag (that is, after the document.writeln state- ment that already uses userAge). Save the file again. Clear the output shown in the error console, then refresh the page in your web browser. The error console should no longer show an error message and the web page should include the line: This is an undeclared variable : undefined
You have learned that in JavaScript a declaration of a variable does not have to precede its use. However, this principle only applies to a particular JavaScript code fragment enclosed in the tags .
h. Remove the declaration
var userAge
home = [" mercury " ," venus " ," earth "]. pop () document. writeln (" \ @5 : " + home + " < br / >") number = [" earth "]. push (" mars "); document. writeln (" \ @6 : " + number + " < br / >")