12 Chapter 3 Javascript Imp Points
12 Chapter 3 Javascript Imp Points
SCRIPTING
There are two types of scripting :
Client-side Scripting:
Server-side Scripting:
1. for…….loop
This loop executes statements as long as condition becomes true, control
comes out from the loop when condition becomes false.
Benefit of for-loop is that it combines initialization, condition and loop
iteration (increment or decrement) in single statement.
Syntax :
for(initialization;condition;iteration)
{
statement block;
}
Textbook Program 1
JavaScript code step by step:
1. for (i = 1; i <= 5; i++):
- `for` is a keyword that starts the loop.
- `(i = 1;` initializes the loop, setting the variable `i` to 1. This is the
starting point of the loop.
- `i <= 5;` is the condition that keeps the loop running. The loop will
continue as long as `i` is less than or equal to 5.
- `i++` increments the value of `i` by 1 after each iteration. This means that
`i` will increase by 1 every time the loop runs.
2. `{`:
- This curly brace `{` marks the beginning of the code block that will be
executed in each iteration of the loop.
3. document.writeln(i);
- `document` is an object that represents the web page.
- `writeln` is a method that writes a line of text to the web page.
- `(i)` is the variable that currently holds the value of `i`.
In each iteration, this value will be written to the web page.
4. `}`:
- This curly brace `}` marks the end of the code block for the loop.
Step-by-Step Execution:
1. The loop starts with `i` set to 1.
2. It checks the condition `i <= 5`. Since 1 is less than or equal to 5, the
condition is true.
3. It executes `document.writeln(i);`, which writes the number 1 to the web
page.
4. After writing the number, `i` is incremented by 1 (so `i` becomes 2).
5. The loop checks the condition `i <= 5` again. Since 2 is less than or equal
to 5, the condition is true.
6. It writes the number 2 to the web page.
7. This process repeats until `i` reaches 6.
8. When `i` is 6, the condition `i <= 5` becomes false, and the loop stops.
So, the output of this code will be the numbers 1, 2, 3, 4, and 5 written in
sequence on the web page.
Textbook Program2
<script>
for (let i = 0; i < 5; i++)
{
document.writeln(i+"<br>");
}
</script>
javascript
<script>
for (let i = 0; i < 5; i++)
{
document.writeln(i + "<br>");
}
</script>
1. <script>:
- This tag tells the browser that the following content is JavaScript code.
3. `{`:
- This curly brace `{` marks the beginning of the code block that will be
executed in each iteration of the loop.
4. **`document.writeln(i + "<br>");`**:
- `document` is an object that represents the web page.
- `writeln` is a method that writes a line of text to the web page.
- `i` is the variable that currently holds the value of `i`. In each iteration,
this value will be written to the web page.
- `+ "<br>"` adds a line break after each number. The `<br>` tag in HTML
creates a line break, so each number will be on a new line.
5. `}`:
- This curly brace `}` marks the end of the code block for the loop.
6. `</script>`**:
- This tag closes the JavaScript code section.
Step-by-Step Execution:
1. The loop starts with `i` set to 0.
2. It checks the condition `i < 5`. Since 0 is less than 5, the condition is true.
3. It executes `document.writeln(i + "<br>");`, which writes the number 0
followed by a line break to the web page.
4. After writing, `i` is incremented by 1 (so `i` becomes 1).
5. The loop checks the condition `i < 5` again. Since 1 is less than 5, the
condition is true.
6. It writes the number 1 followed by a line break to the web page.
7. This process repeats until `i` reaches 5.
8. When `i` is 5, the condition `i < 5` becomes false, and the loop stops.
So, the output of this code will be the numbers 0, 1, 2, 3, and 4, each on a
new line on the web page.
Explanation
for (let i = 0; i < 5; i++) {: This sets up a loop that will run 5 times, with i
taking values from 0 to 4.
Textbook Program 3
for(i=5;i>=1;i--)
{
document.writeln(i);
}
Output:
5
4
3
2
1
Sure, let's break down this JavaScript code step by step:
```javascript
for (i = 5; i >= 1; i--) {
document.writeln(i);
}
```
2. **`{`**:
- This curly brace `{` marks the beginning of the code block that will be
executed in each iteration of the loop.
3. **`document.writeln(i);`**:
- `document` is an object that represents the web page.
- `writeln` is a method that writes a line of text to the web page.
- `i` is the variable that currently holds the value of `i`. In each iteration,
this value will be written to the web page.
4. `}`:
- This curly brace `}` marks the end of the code block for the loop.
Step-by-Step Execution:
1. The loop starts with `i` set to 5.
2. It checks the condition `i >= 1`. Since 5 is greater than or equal to 1, the
condition is true.
3. It executes `document.writeln(i);`, which writes the number 5 to the web
page.
4. After writing the number, `i` is decremented by 1 (so `i` becomes 4).
5. The loop checks the condition `i >= 1` again. Since 4 is greater than or
equal to 1, the condition is true.
6. It writes the number 4 to the web page.
7. This process repeats until `i` reaches 0.
8. When `i` is 0, the condition `i >= 1` becomes false, and the loop stops.
So, the output of this code will be the numbers 5, 4, 3, 2, and 1 written in
sequence on the web page.
2. While…..loop
This loop executes statements as long as the condition is true. As soon as
condition becomes false control comes out of the loop.
Syntax:
initialization;
while(condition)
{
statement block;
iteration
}
3. `{`:
- This curly brace `{` marks the beginning of the code block that will be
executed in each iteration of the loop.
4. `document.writeln(i);`:
- `document` is an object that represents the web page.
- `writeln` is a method that writes a line of text to the web page.
- `i` is the variable that currently holds the value of `i`. In each iteration,
this value will be written to the web page.
5. `i = i + 1;`:
- This line increments the value of `i` by 1. It means that the current value
of `i` is added to 1, and the result is stored back in `i`.
6. `}`:
- This curly brace `}` marks the end of the code block for the loop.
Step-by-Step Execution:
So, the output of this code will be the numbers 1, 2, 3, 4, and 5 written in
sequence on the web page.
Feel free to ask if you need any further clarification or have more questions!
```html
<!DOCTYPE html>
<html>
<head>
<title>Table-I</title>
<script type="text/javascript">
function display() {
var i, a;
a = form1.t1.value;
for (i = 1; i <= 10; i++) {
document.write(a * i + "<br/>");
}
}
</script>
</head>
<body>
<form name="form1">
Enter number to display table:-
<input type="text" name="t1">
<input type="button" value="Display Table" onClick="display()">
</form>
</body>
</html>
```
2. **`<html>`**:
- This tag starts the HTML document.
3. **`<head>`**:
- This section contains meta-information about the document, such as its
title and linked resources like CSS and JavaScript.
4. **`<title>Table-I</title>`**:
- This sets the title of the web page, which appears in the browser tab.
5. **`<script type="text/javascript">`**:
- This tag indicates that the enclosed content is JavaScript code.
6. **`function display()`**:
- This defines a function named `display`. A function is a block of code
designed to perform a specific task when called.
7. **`{`**:
- This curly brace `{` marks the beginning of the function's code block.
8. **`var i, a;`**:
- `var` is a keyword used to declare variables.
- `i` and `a` are variable names.
9. **`a = form1.t1.value;`**:
- This assigns the value entered in the text input (named `t1` in the form
`form1`) to the variable `a`.
11. **`{`**:
- This curly brace `{` marks the beginning of the loop's code block.
13. **`}`**:
- This curly brace `}` marks the end of the loop's code block.
14. **`}`**:
- This curly brace `}` marks the end of the function's code block.
15. **`</script>`**:
- This tag closes the JavaScript code section.
16. **`<body>`**:
- This section contains the content of the web page that is visible to users.
21. **`</form>`**:
- This tag closes the form element.
22. **`</body>`**:
- This tag closes the body section of the HTML document.
23. `</html>`:
- This tag closes the HTML document.
Step-by-Step Execution:
1. When the user enters a number into the text input field and clicks the
"Display Table" button, the `display` function is called.
2. The function retrieves the entered number from the input field and assigns
it to the variable `a`.
3. The function then loops from 1 to 10, multiplying the entered number `a`
by the current loop index `i`.
4. For each iteration, the result of `a * i` is written to the web page, followed
by a line break (`<br/>`).
5. This process generates a multiplication table for the entered number,
displaying each result on a new line.
```html
<!DOCTYPE html>
<html>
<head>
<title>Prime number</title>
<script type="text/javascript">
function display() {
var a, ans;
a = parseInt(form1.t1.value);
ans = 1;
for (i = 2; i < a; i++) {
if (a % i == 0) {
ans = 0;
break;
}
}
if (ans == 1)
alert("Number is prime");
else
alert("Number is not prime");
}
</script>
</head>
<body>
<h1 align="center">Program to check number is prime or not</h1>
<form name="form1" style="text-align:center">
Enter your Number (Greater than one):-<input type="text" name="t1">
<br>
<input type="button" value="check Prime number" onClick="display()">
</form>
</body>
</html>
```
2. `<html>`:
- This tag starts the HTML document.
3. `<head>`:
- This section contains meta-information about the document, such as its
title and linked resources like CSS and JavaScript.
4. **`<title>Prime number</title>`**:
- This sets the title of the web page, which appears in the browser tab.
5. **`<script type="text/javascript">`**:
- This tag indicates that the enclosed content is JavaScript code.
6. **`function display()`**:
- This defines a function named `display`. A function is a block of code
designed to perform a specific task when called.
7. **`{`**:
- This curly brace `{` marks the beginning of the function's code block.
8. **`var a, ans;`**:
- `var` is a keyword used to declare variables.
- `a` and `ans` are variable names.
9. **`a = parseInt(form1.t1.value);`**:
- This line retrieves the value entered in the text input (named `t1` in the
form `form1`) and converts it to an integer using `parseInt()`. The result is
stored in the variable `a`.
12. **`{`**:
- This curly brace `{` marks the beginning of the loop's code block.
14. **`{`**:
- This curly brace `{` marks the beginning of the `if` statement's code block.
16. **`break;`**:
- This statement exits the loop immediately, as there's no need to check
further.
17. **`}`**:
- This curly brace `}` marks the end of the `if` statement's code block.
18. **`}`**:
- This curly brace `}` marks the end of the loop's code block.
21. **`else`**:
- This keyword specifies an alternative block of code to execute if the `if`
condition is not met.
23. **`}`**:
- This curly brace `}` marks the end of the function's code block.
24. **`</script>`**:
- This tag closes the JavaScript code section.
25. **`<body>`**:
- This section contains the content of the web page that is visible to users.
30. **`<br>`**:
- This HTML tag inserts a line break to separate the input field from the
button.
32. **`</form>`**:
- This tag closes the form element.
33. **`</body>`**:
- This tag closes the body section of the HTML document.
34. **`</html>`**:
- This tag closes the HTML document.
Step-by-Step Execution:
1. When the user enters a number into the text input field and clicks the "check
Prime number" button, the `display` function is called.
2. The function retrieves the entered number, converts it to an integer, and
stores it in the variable `a`.
3. The function initializes the variable `ans` to 1, assuming the number is
prime.
4. The loop starts with `i` set to 2 and continues as long as `i` is less than `a`.
5. For each iteration, the function checks if `a` is divisible by `i`. If it is, `ans` is
set to 0, indicating the number is not prime, and the loop breaks.
6. After the loop, the function checks the value of `ans`. If `ans` is still 1, an
alert message stating "Number is prime" is displayed. Otherwise, an alert
message stating "Number is not prime" is displayed.
This code effectively checks if the entered number is a prime number and
displays an appropriate message.
Sr
N Method Description Example
o
<script>
Returns the day
document.write(new
1 getDate() of the month
Date().getDate());
(from 1-31)
</script>
<script>
getFullYe Returns the year document.write(new
3
ar() (four digits). Date().getFullYear());
</script>
<script>
Returns the hour document.write(new
4 getHours()
(from 0-23). Date().getHours());
</script>
<script>
Returns the
getMinute document.write(new
5 minutes (from 0-
s() Date().getMinutes());
59).
</script
<script>
Returns the
getMonth( document.write(new
6 month (from 0-
) Date().getMonth());
11).
</script>
<script>
Returns the
getSecond document.write(new
7 seconds (from 0-
s() Date().getSeconds());
59).
</script>
Returns the <script>
number of document.write(new
8 getTime() milliseconds Date().getTime());
since midnight </script>
Jan 1, 1970. (Ans in milliseconds)
Returns the
<script>
number of
document.write(Date.now());
9 now() milliseconds
</script>
since midnight
(Ans in milliseconds)
Jan 1, 1970.
<script>
Sets the day of
let date = new Date();
10 setDate() the month of a
date.setDate(15); document.write(date);
date object.
</script>
setFullYea Sets the full year
11 <script>
r() of a date object.
let date = new Date();
date.setFullYear(2025);
document.write(date);
</script>
<script>
Sets the hours of let date = new Date();
12 setHours()
a date object. date.setHours(10); document.write(date);
</script>
<script>
setMinute Set the minutes let date = new Date();
13
s() of a date object. date.setMinutes(30); document.write(date);
</script>
<script>
setMonth( Sets the month let date = new Date();
14
) of a date object. date.setMonth(10); document.write(date);
</script>
<script>
setSecond Sets the seconds let date = new Date();
15
s() of a date object. date.setSeconds(45); document.write(date);
</script>
Sets a date to a
specified
let date = new Date();
number of
16 setTime() date.setTime(1634567890123);
milliseconds
document.write(date);
after/before Jan
1, 1970.
Number Object : 3 Property 4 Method
It helps us to work with numbers.
Primitive values (like 34 or 3.14) cannot have properties and methods, but
with JavaScript it is available with primitive values.
Sr Prop
Description
No erty
<script>
document.write
The property represents the zero-based
1 index ([1, 2, 3, 4,
index of the match in the string
5].indexOf(3));
</script>
<script>
document.write
2 length Reflect number of elements in array. ([1, 2, 3, 4,
5].length);
</script>
Sr Meth Example
Description
No od Program
<script>
document.writ
concat Joins two or more arrays, and returns e([1, 2,
1
() a copy of the joined arrays 3].concat([4, 5,
6]));
</script>
document.write
copy
Copies array elements within the array, ([1, 2, 3, 4,
2 Within
to and from specified positions. 5].copyWithin(
()
0, 3));
<script>
document.write
([1, 2, 3, 4,
Returns the value of the first element in 5].find(element
3 find()
an array that satisfies a test in testing. => element >
3))
</script>
Ans : 4
<script>
[1, 2, 3, 4,
5].forEach(ele
forEac
4 Calls a function for each array element. ment =>
h()
document.write
(element));
</script>
<script>
document.writ
index Search the array for an element and
5 e([1, 2, 3, 4,
Of() returns its position.
5].indexOf(3));
</script>
<script>
document.write
isArra
6 Checks whether an object is an array. (Array.isArray(
y()
[1, 2, 3]));
</script>
<script>
document.writ
Removes the last element of an array,
7 pop() e([1, 2, 3, 4,
and returns that element.
5].pop());
</script>
<script>
document.writ
Adds new elements to the end of an
8 push() e([1, 2, 3,
array, and returns the new length
4].push(5));
</script>
<script>
document.write
revers Reverses the order of the elements in an
9 ([1, 2, 3, 4,
e() array.
5].reverse());
</script>
<script>
document.write
([5, 3, 8, 1,
10 sort() Sorts the elements of an array.
2].sort((a, b) =>
a - b));
</script>
Summary
JavaScript is light weight scripting language.
JavaScript is platform independent language.
There are two types of scripts; client side script and server side scripts.
Client side
scripts reside on client machine and server side script resides on web
server.
JavaScript provide ‘switch…case’ as multi way decision statement.
For....loop, while…loop and do…while are commonly used looping
structures in JavaScript.
DOM (Document Object Model) is a programming interface for HTML
and XML documents. It defines logical structure of document.
Window object is parent object of all other objects hence its methods
can be used without specifying it.
JavaScript is event based language support objects events such as
onBlur, onFocus, onChange, onSelect, onSubmit, onLoad, onUnload,
onResize etc.
JavaScript supports built-In objects such as Date, String, Math, Number
and array etc.
These objects contain number of properties and methods that are useful
while creating interacting web pages.
Note : islnteger() is supported by version Mozilla Firefox 16 and higher.
Journal Program
Practical 8 Output
Practical 8
Coding --> Filename: Color1.html
<!-- Filename Color1.html-->
<!DOCTYPE html>
<html>
<title>
Display of 7 different Colors
</title>
<script> function f1()
{
document.bgColor="red";
window.setTimeout("f2()",1200);
}
function f2()
{
document.bgColor="yellow";
window.setTimeout("f3()",1200);
}
function f3()
{
document.bgColor="brown";
window.setTimeout("f4()",1200);
}
function f4()
{
document.bgColor="green";
window.setTimeout("f5()",1200);
}
function f5()
{
document.bgColor="purple";
window.setTimeout("f6()",1200);
}
function f6()
{
document.bgColor="pink";
window.setTimeout("f7()",1200);
}
function f7()
{
document.bgColor="skyblue";
window.setTimeout("f1()",1200);
}
function msg()
{
alert("Display of colors page closed by user");
}
</script>
<body onload="f1()" onUnload="msg()">
<h1 align=center>
Display of 7 Different colors on load event</h1>
</body>
</html>
Practical 8 Output
Practical 9 Question
Create event driven JavaScript program for the following. Make use
of appropriate variables, JavaScript inbuilt string functions and
control structures. To accept string from user and count number of
vowels in the given string.
Practical 9 Ans
Coding Filename: vowel.html
Ans
<!-- File name vowel.html-->
<!DOCTYPE html>
<html>
<head>
<title>Vowel Count </title>
</head>
<body>
<form name="form1">
<h1> Enter the String whose vowel will be counted</h1>
<input type="text" name="text1">
<input type="button" name="vowel" value="Click to count"
onclick=count()>
</form>
<script>
function count()
{
var i,ch,str,counter=0;
str=form1.text1.value;
for(i=0;i<str.length;i++)
{
ch=str.charAt(i);
if(ch=='A'||ch=='a'||ch=='E'||ch=='e'||ch=='I'||ch=='i'||ch=='O'||ch=='o'||
ch=='U'||ch=='u')
counter++;
}
alert("Number of Vowels in Entered String is : "+counter);
}
</script>
</html>
Practical 9 Output
Practical 10 Question
Create JavaScript program which compute the average marks of
students. Accept six subject marks of student from user. Calculate
average marks of student which is used to determine the
corresponding grades.
Range Grade
35 to 60 F
61 to 70 D
71 to 80 C
81 to 90 B
91 to 100 A
Practical 10 : Ans :
<!-- Filename Grade.html-->
<!DOCTYPE html>
<html>
<head>
<title> Student Grade </title>
</head>
<body>
<h2 align="center"> Student Grade </h2>
<form name="frm1">
Enter Marks of English : <input type="number" name="t1"><br><br>
Enter Marks of Maths : <input type="number" name="t2"><br><br>
Enter Marks of Physics : <input type="number" name="t3"><br><br>
Enter Marks of Chemistry : <input type="number" name="t4"><br><br>
Enter Marks of Biology : <input type="number" name="t5"><br><br>
Enter Marks of I.T : <input type="number" name="t6"><br><br>
<input type="button" name="bt1" value="Get Grade" onClick="grade()">
</form>
<body>
<script>
function grade()
{
var s1,s2,s3,s4,s5,s6,total,avg;
s1=parseInt(frm1.t1.value)
s2=parseInt(frm1.t2.value)
s3=parseInt(frm1.t3.value)
s4=parseInt(frm1.t4.value)
s5=parseInt(frm1.t5.value)
s6=parseInt(frm1.t6.value)
total=s1+s2+s3+s4+s5+s6;
avg=total/6;
alert("Total Marks of the Student is : "+total);
alert("Average Marks of the Student is : "+avg);
if(avg<60)
{
alert("Grade F");
}
else if(avg<70)
{
alert("Grade D");
}
else if(avg<80)
{
alert("Grade C");
}
else if(avg<90)
{
alert("Grade B");
}
else if(avg<=100)
{
alert("Grade A");
}
else
{
alert("Incorrect Entry")
}
}
</script>
</html>
Practical 10 Output
3. ___ and ____are event /event handler used with text object in
JavaScript.
a)onBlur b) onMove c)onFocus d) onAction
Ans : a)onBlur c)onFocus
MARKS DISTRIBUTION
Chapter 3 Advance Javascript marks distribution
Q.Type A.JS
Q1 FiB 2
Q2 T/F 2
Q3 MCQ1 1
Q4 MCQ2 4
Q5 MCQ3 0
Q6 match 1
Q7 short
10
Q8 prg 5 or 5
15