IT1100 IWT - Labsheet 04
IT1100 IWT - Labsheet 04
Objectives
By the end of this lab session, you will familiarize with basic programming constructs and
functions in scripting languages using JavaScript (JS).
Introduction
• JavaScript is a scripting language for web-based systems.
• It can calculate, manipulate, validate data and change content in both HTML and CSS.
• There are three ways to use JS in an HTML page.
o JS in <head>
It adds JS content inside <script> tag which is in inside of the <head>.
o JS in <body>
Write functions inside body
o Using an external file (preferred)
• All JavaScript variables must be identified with unique names along with ‘var’ keyword.
Ex: var person = "John Doe"
• There are 2 basic types of JS arrays.
o Indexed Arrays – Access elements using numerical indexes.
Exercise 1:
• Go to the IWT folder structure which you created on the first lab session.
• Create a file named “myScript.js” inside the “IWT/js” folder.
• Create a file named “news.html” and save it inside “IWT/src” folder.
• Open the “index.html” file.
• Link that file to the “NEWS” button in the index.html
BSc (Hons) in Information Technology
Year 1, Semester II, 2024
IT1100 – Internet and Web Technologies
• Copy the entire code in the “index.html” file and paste it in the “news.html”.
• Delete the content between navigation bar and the footer.
• Fill the following boxes with the relevant codes for the given activities and edit the
“news.html” file.
I. Display the Date
• Display the system date as a text in the top of the web page.
• Write JS inside the <head> tag.
https://www.w3schools.com/jsref/met_doc_write.asp
If/else - https://www.w3schools.com/js/js_if_else.asp
String comparison - https://www.w3schools.com/js/js_comparisons.asp
Change text - https://www.w3schools.com/js/js_htmldom_html.asp
Change image - https://www.w3schools.com/jsref/prop_img_src.asp
BSc (Hons) in Information Technology
Year 1, Semester II, 2024
IT1100 – Internet and Web Technologies
III. Loops
a) For Loop
• Create a button named “average Price” in news.html.
• Create a function called priceForLoop() in myScript.js.
• Create an array with product name and price in the function.
Ex – [“product X - $100”, “product Y - $200”];
Arrays - https://www.w3schools.com/js/js_arrays.asp
• Display the array elements using a for loop as shown below.
For loop - https://www.w3schools.com/js/js_loop_for.asp
BSc (Hons) in Information Technology
Year 1, Semester II, 2024
IT1100 – Internet and Web Technologies
b) For In Loop
• Redo the above function using a for-in loop and name the function as
“productForInLoop()”.
• Display the data as the above given example.
https://www.w3schools.com/jsref/jsref_forin.asp
BSc (Hons) in Information Technology
Year 1, Semester II, 2024
IT1100 – Internet and Web Technologies
c) For In Loop
• Create a button named “Prices higher than 1000”.
• Create a function named “priceHigher()” in the myScript.js file.
• Call that function in the button click event.
• Create an array which contains at least 5 values (at least 2 must be higher
than 1000 and 2 must be lower).
• Display the array elements which have the prices higher than 1000.
d) For In Loop
• Create a button named “Prices lower than 1000”.
• Create a function named “priceLower()” in the myScript.js file.
• Call that function when the button click event.
Copy and paste the array which has created in the above exercise.
• Display the array elements which have the prices lower than 1000.
BSc (Hons) in Information Technology
Year 1, Semester II, 2024
IT1100 – Internet and Web Technologies
Exercise 2: Write a program to calculate the sum of first 10 natural numbers. (While loop)