JavaScript Coding Exercises: Hello World and Equality Comparisons, Lecture notes of Programming Languages

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

2011/2012

Uploaded on 07/16/2012

sambandan
sambandan 🇮🇳

4.7

(3)

35 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
<ht ml > <h ead > < tit le > He ll o Wo rl d </ ti tle > </ hea d >
<bo dy >
<p > O u r f i rs t J av a S cr i p t s c ri pt < /p >
< sc r ip t t yp e =" t e xt / j av a sc r ip t " >
us e r = "{ y ou r n am e }" ;
d oc u m en t . wr it e l n (" < p > < b > H el l o " + u se r +
" < br / > \ n He l l o W or l d ! </ b > < / p >" )
</ s cr i pt >
<p > T h i s is t he r es t o f t he pa g e < / p >
< nos cript > Jav aS cript n ot s upport ed or ena bled </ nosc ript >
</ b od y >< / ht m l >
Replace {your name} with your own name.
1
docsity.com
pf3
pf4

Partial preview of the text

Download JavaScript Coding Exercises: Hello World and Equality Comparisons and more Lecture notes Programming Languages in PDF only on Docsity!

< 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 enabled

Replace {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

  1. Replicate the array example we have seen in the lecture, that is create a web page that executes the following JavaScript code: planets = [" earth "] planets. unshift (" mercury " ," venus ") planets. push (" mars " ," jupiter " ," saturn "); document. writeln (" planets \ @1 : " + planets. join (" ") + " < br / >") last = planets. pop () document. writeln (" planets \ @2 : " + planets. join (" ") + " < br / >") first = planets. shift () document. writeln (" planets \ @3 : " + planets. join (" ") + " < br / >") document. writeln (" \ @4 : " + first + " " + last + " < br / >")

home = [" mercury " ," venus " ," earth "]. pop () document. writeln (" \ @5 : " + home + " < br / >") number = [" earth "]. push (" mars "); document. writeln (" \ @6 : " + number + " < br / >")