Professional Documents
Culture Documents
11
TVL-ICT
PROGRAMMING .NET TECHNOLOGY NC
III
QUARTER 2 – MODULE 3
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 create function in JavaScript.
learn how to call a function.
learn how to identify and use JavaScript variable scope in more depth.
learn how to use JavaScript operators.
In the previous lesson, you have learned the JavaScript syntax and how to insert it into
HMTL document. Most web applications that solve real-world problems are much larger than
the trivial project that are presented in the previous module. Real-world apps require hundreds
of thousands or even many millions of lines of code to perform several tasks and to make it
modern program.
II. CONTENT
Web developers develop and maintain a large web app by constructing it from small,
simple pieces. They modularize the app by separating its tasks into self contained units. There
are several motivations for modularizing an app by means of methods or function. One is
the divide and conquer approach.
In this module, we study the functions in simple way. We emphasize how to create
user-define functions, declare and use functions to facilitate the design, implementation,
operation and maintenance of web apps in JavaScript coding.
A function is a group of reusable code which can be called anywhere in your program.
This eliminates the need of writing the same code again and again. It helps programmers in
writing modular codes. Functions allow a programmer to divide a big program into a number
of small and manageable functions.
(https://www.tutorialspoint.com/javascript/javascript_functions.htm)
JavaScript also supports all the features necessary to write modular code using
functions like Java, C#, and other programming languages. You have already seen functions
like alert() and write() in the earlier module. We were using these functions again and again,
but they had been written in core JavaScript only once. JavaScript allows us to write our own
functions as well.
function functionName(parameter) {
//statements
}
</script>
To define a function in JavaScript we use the function keyword inside the script element,
followed by a unique function name, a list of parameters (that might be empty), and a
statement block surrounded by curly braces.
1
Sample function to display message in pop-window.
function ShowMessage() {
alert( 'Hello SHS ICT!' );
}
Our new function can be called by its name: ShowMessage()
<script type = "text/javascript">
function ShowMessage() {
alert( 'Hello SHS ICT!' );
}
ShowMessage(); //calling the function.
</script>
Variable
In the previous lesson, you have learned how to declare variables using the var
keyword. Storing a value in a variable is called variable initialization. Use the var keyword
only for declaration or initialization, once for the existence of any variable name in a
document. You should not re-declare same variable twice. JavaScript variable can hold a
value of any data type. Unlike Java and C# languages, you don't have to tell JavaScript
during variable declaration what type of value the variable will hold.
JavaScript Variable Scope
The scope of a variable is the region of your program in which it is defined.
Global Variables − A global variable has global scope which means it can be defined
anywhere in your JavaScript code.
Local Variables − A local variable will be visible only within a function where it is defined.
Function parameters are always local to that function.
2
A. Global Variable declaration
var userName = “Beshi”;
function ShowMessage() {
var message = 'Hello, ' + userName;
alert(message);
}
ShowMessage(); // Hello, Beshi
3
The following code shows how to use arithmetic operators in JavaScript.
<html>
<body>
function Compute(){
var a = 33;
var b = 10;
var c = "Test";
var linebreak = "<br />";
document.write("a + b = ");
result = a + b;
document.write(result);
document.write(linebreak);
document.write("a - b = ");
result = a - b;
document.write(result);
document.write(linebreak);
document.write("a / b = ");
result = a / b;
document.write(result);
document.write(linebreak);
document.write("a % b = ");
result = a % b;
document.write(result);
document.write(linebreak);
document.write("a + b + c = ");
result = a + b + c;
document.write(result);
document.write(linebreak);
a = ++a;
document.write("++a = ");
result = ++a;
document.write(result);
document.write(linebreak);
b = --b;
document.write("--b = ");
result = --b;
document.write(result);
document.write(linebreak);
}
Compute();
</script>
</body>
</html>
4
Output
a + b = 43
a - b = 23
a / b = 3.3
a%b=3
a + b + c = 43Test
++a = 35
--b = 8
III. EXERCISES
<html>
<head>
<title> Basic JavaScript </title>
<script type= “text/javascript”>
function sms(){
</script>
</head>
<body>
</body>
</html>
IV. SUMMATIVE
Write all your answers in the ANSWER SHEET attached with this module.
5
a. var “student”
b. var student
c. student
d. var
_____3. To assign the value of the variable gender, the correct syntax is
a. var gender= “FEMALE”
b. gender var
c. var
d. alert(gender)
_____4. To declare a variable y with a value of 10, the syntax is
a. var y=10;
b. y;10
c. y:10
d. y+10
_____5. The following syntax are correct except one.
a. var y=5;
b. var y;
c. integer x;
d. xy
_____6. What keyword use to declare a variable?
a. var
b. :
c. =
d. me
_____7. The keyword use to create a method name person.
a. script
b. function
c. alert()
d. body
_____8. Which of the following syntax is correct?
a. var person()
b. function person() { }
c. var person John
d. none of the above
_____9. To display a message that will pop on the browser
a. innerHTML
b. var
c. alert();
d. window
_____10. Declare variable x with value of 10.
6
a. var x=10;
b. var x 10
c. x==10
d. var 10 x
PERFORMANCE TASK
Direction: Write a JavaScript function name Counter to display numbers from 1 to 10. The
numbers should be displayed in element <p id= “test”></p>
7
NAME: ___________________________________ SCORE: ________
SUBJECT TEACHER: _______________________ GRADE/SECTION: ________
A.
1. __________ 6. __________
2. __________ 7. __________
3. __________ 8. __________
4. __________ 9. __________
5. __________ 10. __________
B.
1. __________
2. __________
3. __________
4. __________
5. __________
PERFORMANCE TASK
8
ANSWER KEY
Exercises
A.
<html>
<head>
<title> Basic JavaScript </title>
<script type= “text/javascript”>
function sms(){
alert(“JavaScript is fun”)
}
</script>
</head>
sms();
<body>
</body>
</html>
Exercise B.
<html>
<head>
<title> Basic JavaScript </title>
<script type= “text/javascript”>
function computeSum(){
var a;
var b;
var c;
c=a+b;
}
computeSum();
</script>
</head>
<body>
</body>
</html>
References: