Web Application - Removed
Web Application - Removed
min()
1. What is JavaScript?
Answer – ECMAScript is yet another name for JavaScript (other names include
LiveScript). The current JavaScript that you see supported in browsers is ECMAScript
a. Browser Support – All the browsers support the JavaScript program without using
any plugins.
b. Client Side and Server Side – JavaScript programs can be written in Client Side and
Server Side in both conditions.
c. Functional Programming Language – In JavaScript support function program,
function just like a data type. Function can accept parameters and can also return the
value.
d. Support for Objects – JavaScript handles the objects easily like Array, String and
Math. It also supports Inheritance concepts just like C++ and Java programs.
Answer –
a. Number – JavaScript uses a double-precision 64-bit representation for numbers.
Addition, subtraction, multiplication, division, modulus etc.
b. String – Strings are character sequences in JavaScript. They’re actually sequences of
Unicode characters, each of which is represented by a 16-bit number.
c. Boolean – The boolean type in JavaScript has two potential values: true and false.
d. Null – You can give a variable the value null to indicate that it does not have a value
right now but will later. A null value denotes the absence of a value.
e. Undefined – When no value is assigned to a variable or object before it is used, it has
an undefined value.
Answer – If the argument does not appear to be a number, the isNaN function will
return TRUE (1).
Answer – The term “undefined” refers to a variable that has been declared but not yet
given a value. Null, on the other hand, is a value assigned to a variable. It can be
assigned to a variable as a no-value representation.
Answer – The Document Object Model (DOM) is an HTML and XML programming
interface. It specifies the logical structure of documents as well as how they are accessed
and modified.
Answer –
a. Client – side – Client-side scripting is carried out by web browsers. When all of the
code is present in the browser, it is used. The source code is used to transport data from
a web server to a user’s machine via the internet and run it directly in browsers. It’s also
utilised for validations and user event functions.
b. Server – side – Server-side scripting is carried out by web servers. Basically, they’re
used to make dynamic pages. It also has access to the web server’s file system. A
webserver is a server-side environment that runs on a server side.
14. What is the difference between entry control loop & exit control loop?
Answer – Loops are a technique for repeatedly repeating a group of statements until a
condition is met. There are three types of loops in the JavaScript programming
language: 1) while loop, 2) do while loop, and 3) for loop.
As a result, loops can be managed in two ways: at the entry level or at the exit level.
Answer – The interaction between JavaScript and HTML is handled via events that occur
when the user or the browser manipulates a page. Event Handling refers to the process
of reacting to situations. When the page loads, the mouse moves on the page, the user
clicks on the button these all are examples of events.
Different types of event in JavaScript
a. onLoad – occurs when a page loads in a browser
b. onUnload – occurs just before the user exits a page
c. onMouseOver – occurs when you point to an object
d. onMouseOut – occurs when you point away from an object
e. onSubmit – occurs when you submit a form
f. onClick – occurs when an object is clicked
Web Scripting JavaScript Class 12
Control Statements
If, else, else if, and switch-case statements are all conditional statements in JavaScript.
They’re used to check for a specific condition and then run the code based on the result.
// Write a program to accept two numbers from the user and find the greatest
numbers
<html>
<body>
<script type="text/javascript">
var num1, num2;
num1=parseInt(prompt("Enter 1st Number"));
num2=parseInt(prompt("Enter 2nd Number"));
if(num1 > num2)
{
document.write("1st Number is greatest");
}
else
{
document.write("2nd Number is greatest")
}
</script>
</body>
</html>
// Write a program to accept numbers from the user and check the number is even
or odd
<html>
<body>
<script type="text/javascript">
var num1;
num1=parseInt(prompt("Enter Number"));
if(num1%2==0)
{
document.write("Number is even");
}
else
{
document.write("Number is Odd")
}
</script>
</body>
</html>
// Write a program to accept three numbers from the user and find the greatest
numbers
<html>
<body>
<script type="text/javascript">
var num1, num2, num3;
num1=parseInt(prompt("Enter 1st Number"));
num2=parseInt(prompt("Enter 2nd Number"));
num3=parseInt(prompt("Enter 3rd Number"));
if(num1 > num2 && num1 > num3)
{
document.write("1st Number is greatest");
}
else if(num2 > num3)
{
document.write("2nd Number is greatest");
}
else
{
document.write("3rd Number is greatest")
}
</script>
</body>
</html>
// Write a JavaScript program to check vowels or consonants using switch cases.
<html>
<body>
<script type="text/javascript">
var ch;
ch=prompt("Enter any small character");
switch(ch)
{
case 'a':
document.write("Vowel");
break;
case 'e':
document.write("Vowel");
break;
case 'i':
document.write("Vowel");
break;
case 'o':
document.write("Vowel");
break;
case 'u':
document.write("Vowel");
break;
default:
document.write("Consonant");
}
</script>
</body>
</html>
Web Scripting JavaScript Class 12
Looping Statements
Using a loop you can execute the statement multiple times.
1. For loop
2. While loop
3. Do-While loop
For Loop
<html>
<body>
<script type="text/javascript">
var i;
for(i=1;i<=10;i++)
{
document.write(i);
}
</script>
</body>
</html>
// Write a program to print the number from 10 to 1 using for loop
<html>
<body>
<script type="text/javascript">
var i;
for(i=10;i>=1;i--)
{
document.write(i);
}
</script>
</body>
</html>
// Write a program to print the even numbers from 1 to 100 using for loop
<html>
<body>
<script type="text/javascript">
var i;
for(i=1;i<=100;i++)
{
if(i%2==0)
{
document.write(i);
}
}
</script>
</body>
</html>
// Write a program to accept 10 numbers from the user and find the greatest
number using a for loop.
<html>
<body>
<script type="text/javascript">
var i, num, max;
max=parseInt(prompt("Enter Number"));
for(i=1;i<=10;i++)
{
num=parseInt(prompt("Enter Number"));
if(num > max)
{
max = num;
}
}
document.write("Greatest Number is " + max);
</script>
</body>
</html>
// Write a program to accept 10 numbers from the user and sum of numbers using
a for loop.
<html>
<body>
<script type="text/javascript">
var i, num, sum=0;
for(i=1;i<=10;i++)
{
num=parseInt(prompt("Enter Number"));
sum=sum+num;
}
document.write("Sum is " + sum);
</script>
</body>
</html>
While Loop
// Write a program to print the number from 1 to 10 using a while loop.
<html>
<body>
<script type="text/javascript">
var i;
i=1;
while(i<=10)
{
document.write(i);
i++;
}
</script>
</body>
</html>
Do-While Loop
// Write a program to print the number from 1 to 10 using a do-while loop.
<html>
<body>
<script type="text/javascript">
var i;
i=1;
do
{
document.write(i);
i++;
}while(i<=10);
</script>
</body>
</html>
Web Scripting JavaScript Class 12
Array in JavaScript
Array is a group of variables which have the same name.
There are three different ways to declare array variables.
a. var a = new Array();
b. var a = [10];
c. var a =[“Apple”, “Mangoes”, “Orange”, “Banana”];
First Method
// Write a JavaScript program to accept 10 numbers from the user and print the
numbers using an array.
<html>
<body>
<script type="text/javascript">
var a = new Array();
var i;
for(i=0;i<=9;i++)
{
a[i]=parseInt(prompt("Enter Number"));
}
for(i=0;i<=9;i++)
{
document.write(a[i]);
}
</script>
</body>
</html>
Second Method
// Write a JavaScript program to accept 10 numbers from the user and print the
numbers using an array.
<html>
<body>
<script type="text/javascript">
var a = [10];
var i;
for(i=0;i<=9;i++)
{
a[i]=parseInt(prompt("Enter Number"));
}
for(i=0;i<=9;i++)
{
document.write(a[i]);
}
</script>
</body>
</html>
Third Method
// Write a JavaScript program to print the number from 1 to 5.
<html>
<body>
<script type="text/javascript">
var a = ["1", "2", "3", "4", "5"];
var i;
for(i=0;i<=4;i++)
{
document.write(a[i]);
}
</script>
</body>
</html>
Web Scripting JavaScript Class 12
String in JavaScript
Combination of characters is know as string.
// Write a program to display the middle value from the string using substring.
<html>
<body>
<script>
var str, result;
str=prompt("Enter String");
result=str.substring(1, 4);
document.write(result);
</script>
</body>
</html>
Answer -
Input - INTERNATIONAL
Output - NTE
<html>
<body>
<script>
var str1, str2, result;
str1=prompt("Enter String");
str2=prompt("Enter String");
result=str1.concat(str2);
document.write(result);
</script>
</body>
</html>
Answer -
Output - HelloWorld
<html>
<body>
<script>
var str=["Hello", "World"];
var result;
result=str.join();
document.write(result);
</script>
</body>
</html>
Answer -
Input - str=[“Hello”,”World”];
Output - Hello, World
<html>
<body>
<script>
var str=["Welcome", "to", "New", "World"];
str.pop()
document.write(str);
</script>
</body>
</html>
Answer -
Input - str=["Welcome", "to", "New", "World"];
Output - Welcome,to,New
Note - Pop is used to remove the last element from the array.
// Write a program to add the last element in the array string.
<html>
<body>
<script>
var str=["Welcome", "to", "New"];
str.push("World");
document.write(str);
</script>
</body>
</html>
Answer -
Input - str=["Welcome", "to", "New"]; str.push("World");
Output - Welcome,to,New,World
Note - In the array you can add new element in the last
// Write a program to display the array elements in reverse order.
<html>
<body>
<script>
var str=["Welcome", "to", "New", "World"];
str.reverse();
document.write(str);
</script>
</body>
</html>
Answer -
Input - str=["Welcome", "to", "New", "World"];
Output - World,New,to,Welcome
// Write a program to remove the first element from the array.
<html>
<body>
<script>
var str=["Welcome", "to", "New", "World"];
str.shift();
document.write(str);
</script>
</body>
</html>
Answer -
Input - str=["Welcome", "to", "New", "World"];
Output - to,New,World
// Write a program to remove the first element from the array.
<html>
<body>
<script>
var str=["Banana", "Orange", "Lemon", "Apple", "Mango"];
var result;
result = str.slice(1, 4);
document.write(result);
</script>
</body>
</html>
Answer -
Input - str=["Banana", "Orange", "Lemon", "Apple", "Mango"];
Output - Orange,Lemon,Apple
<html>
<body>
<script>
var str=["Banana", "Orange", "Lemon", "Apple", "Mango"];
var result;
result = str.sort();
document.write(result);
</script>
</body>
</html>
Answer -
Input - str=["Banana", "Orange", "Lemon", "Apple", "Mango"];
Output - Apple,Banana,Lemon,Mango,Orange
// Write a program to add the new element in the array.
<html>
<body>
<script>
var str=["Banana", "Orange", "Lemon", "Apple", "Mango"];
var result;
str.splice(2, 0, "Lichi", "Kiwi");
document.write(str);
</script>
</body>
</html>
Answer -
Output - Banana,Orange,Lichi,Kiwi,Lemon,Apple,Mango
Note - Splice will add the new element after two element
// Write a program to add the new element in the beginning of the array.
<html>
<body>
<script>
var str=["Banana", "Orange", "Lemon", "Apple", "Mango"];
var result;
str.unshift("Lemon", "Kiwi");
document.write(str);
</script>
</body>
</html>
Answer -
Output - Lemon,Kiwi,Banana,Orange,Lemon,Apple,Mango
Note - unshift allows you to add a new element before the first array element.
Web Scripting JavaScript Class 12
Function in JavaScript
In JavaScript, functions can be declared using the function keyword, followed by the
function name, and, in parentheses, the list of parameters (without a var declaration).
The actual function definition is enclosed in curly brackets. The return keyword is used
to return a value or just terminate the function.
Syntax –
function function-name(parameter-list)
{
declarations and statements
}
// Write a program to accept two numbers from the user and find the sum of two
numbers using function;
<html>
<body>
<script>
function sum(x, y)
{
var sum;
sum=x+y;
return sum;
}
var num1, num2, result;
num1=parseInt(prompt("Enter Number"));
num2=parseInt(prompt("Enter Number"));
result = sum(num1, num2);
document.write("Sum is " + result);
</script>
</body>
</html>
Web Scripting JavaScript Class 12
Math in JavaScript
// Write a program to accept decimal values from the user and roundup the value.
<html>
<body>
<script>
var num, result;
num=parseFloat(prompt("Enter Number"));
result = Math.round(num)
document.write(result);
</script>
</body>
</html>
// Write a program to display any random value from 0 to 1.
<html>
<body>
<script>
var result;
result = Math.random();
document.write(result);
</script>
</body>
</html>
// Write a program to find the maximum value from 5 numbers.
<html>
<body>
<script>
var result;
result = Math.max(54, 38, 71, 44, 92);
document.write(result);
</script>
</body>
</html>
// Write a program to find the minimum value from 5 numbers.
54, 38, 71, 44, 92
<html>
<body>
<script>
var result;
result = Math.min(54, 38, 71, 44, 92);
document.write(result);
</script>
</body>
</html>
Events in JavaScript
// Onload event
<html>
<script>
function myFunction()
{
confirm("Welcome to the browser");
}
</script>
<body onload="myFunction()">
</body>
</html>
// OnClick event
<html>
<script>
function myFunction()
{
confirm("You have clicked on the button");
}
</script>
<body>
<button onclick="myFunction()">Click me</button>
</body>
</html>