Q2 MODULE5 G11 .NET PROG MangaldanNHS
Q2 MODULE5 G11 .NET PROG MangaldanNHS
11
TVL-ICT
PROGRAMMING .NET TECHNOLOGY
NC III
QUARTER 2 – MODULE 5
Create HTML5 document using advanced
techniques with JavaScript and CSS3
Write JavaScript code that manipulates
the HTML DOM and handle events
I. INTRODUCTION
In this lesson you will:
learn how to use JavaScript Date object.
learn how to use conditional statements (if else).
learn how to use switch statement to perform a multiway branch.
learn how to create JavaScript Array.
learn how to use for loop to execute a block of code a number of
times.
In the previous lesson, you have learned the API document object model for
manipulating HTML. Manipulating the DOM allows you to create applications that update the
data of the page without needing a refresh. Also, you can create applications that are
customizable by the user and then change the layout of the page without a refresh. You can
drag, move, and delete elements. As we continue in this lesson, the next topics are using
how to use date object, statements and the loops in JavaScript.
II. CONTENT
Date Object
JavaScript provides Date object to work with date & time including days, months, years,
hours, minutes, seconds and milliseconds. The following example shows how to display
current date and time using Date object in JavaScript.
Conditional Statements
1
JavaScript includes following forms of if-else
conditions:
1. if condition
2. if-else condition
3. else if condition
The 'if...else' statement is the next form of control statement that allows JavaScript to
execute statements in a more controlled way. Use else statement when you want to execute
the code every time when if condition evaluates to false.
Use "else if" condition when you want to apply second level condition after if statement.
var myGrade = 95;
var passGrade = 75;
{
alert("Sorry. You are failed.");
}
2
//Next level condition
else if( myGrade == passGrade)
{
alert("Congratulations!");
}
Switch Statement
The switch is a conditional statement like if statement. Switch is useful when you
want to execute one of the multiple code blocks based on the return value of a specified
expression.
This example uses the weekday number to calculate the weekday name:
JavaScript Array
You have learned that a variable can hold only one value, for example var i = 1,
we can assign only one literal value to i. We cannot assign multiple literal values to a
variable i. To overcome this problem, JavaScript provides an array.
An array is a special type of variable, which can store multiple values using special
syntax. Every value is associated with numeric index starting with 0. The following figure
illustrates how an array stores value.
3
Values 100 25 Hello 300 42 10 -12 1 47 2
0 1 2 3 4 5 6 7 8 9
Index
Lower Upper
Bound Bound
The following example shows how to define and initialize an array using array literal
syntax.
Array Constructor
You can initialize an array with Array constructor syntax using new keyword.
var stringArray = new Array();
stringArray[0] = "one";
stringArray[1] = "two";
stringArray[2] = "three";
stringArray[3] = "four";
Array includes "length" property which returns number of elements in the array.
Use for loop to access all the elements of an array using length property.
For Loops
JavaScript includes for loop like Java or C#. Use for loop to execute code
repeatedly.
The for loop requires following three parts.
01234
III. EXERCISES
<script>
var user= “ADMIN”
var pw= “12345”
var inputUser= ____________________________________;
var inputPassword= ____________________________________;
</script>
<body>
<input type = “text” id= “user” />
<input type = “text” id= “pw” />
5
</body>
1. Create a Date object and alert the current date and time.
var dt = ____________
alert(dt);
2. Use the correct Date method to extract the year out of a date object.
var year;
var dt=new Date();
year= _____________
3. Use the correct Date method to get the month (0-11) out of a date object.
var month;
var dt=new Date();
month= _____________
4. Use the correct Date method to set the year of a date object to 2020.
var dt=new Date();
dt= _____________
IV. SUMMATIVE
Write all your answers in the ANSWER SHEET attached with this module.
B. Directions: Complete the source code below. Using for loop, iterate through
this array studentList = ["Arthan", "Dexter", "Erica", "Marcel"] to display all the
content in element <p id= “txt”>.
<body>
<h2>JavaScript For Loop</h2>
6
<p id="txt"></p>
<script>
var studentList = ["Arthan", "Dexter", "Erica", "Marcel"];
var i;
var names=" ";
for(i=________; i< ________; i++){
</body>
PERFORMANCE TASK
7
NAME: ___________________________________ SCORE: ________
SUBJECT TEACHER: _______________________ GRADE/SECTION: ________
A.
1.
2.
B.
Performance Task
8
ANSWER KEY
EXERCISES
A.
<script>
var user= “ADMIN”
var pw= “12345”
var inputUser= document.getElementById(“user”).value;
var inputPassword= document.getElementById(“pw”).value;
</script>
<body>
<input type = “text” id= “user” />
<input type = “text” id= “pw” />
</body>
B.
1. var dt= new Date();
2. dt.getFullYear();
3. month = dt.getMonth();
4. dt.setFullYear(2020);
5. dt. toDateString();
References: