Vks Javascript
Vks Javascript
JAVASCRIPT
External JavaScript file can also be added using src attribute with <Script> Tag
<Script Language= “JavaScript” src= “External.js”>
Script code….
</Script>
Q6. What is the difference between = ,== & === operator explain with example
= operator is used to assign value
== operator is used compare two operand for equality
=== is also used to compare to operand strictly for same type
Example
0==false // results in true as false is equivalent 0
2==”2” // results in true as string automatically converted to int
But
0===false // false as data type are different for operand
2===”2” // false as data type are different for operand
prompt() this function ask user to input some information and store that in a
variable
Syntax Variable=prompt(“Message”)
var name=prompt(“”Enter Your Name”)
confirm() function display a confirm dialogue box and ask user user to click either
ok or cancel to
respond to question. It return true if Ok button is clicked and false if
Cancel button is clicked
Syntax variable=confirm(“Message”)
var choice= (“are you Vegetarian”)
Actual Parameter : Are the actual variable/Constant passed during function is called.
It can be variable constant or expression resulting a value
Function add ( A, B) // A & B are formal parameter
{
var sum=A+B;
alert(sum);
}
var x=10;
var y =20;
add(x,y)// x, y are actual parameter
add(20,30)// passing constant as actual parameter
add(2*x, y-2) //passing expression as actual parameter
parseInt() Convert the string to Number if number is in the form of string if there
is no number at the beginning of string , “NaN” is return
document.write(parseInt(“123.45”)); //return 124
document.write(parseInt(“abcdef”)); // return NaN
document.write(parseInt(“216.ABC”)); // return 216
round() It is used to round off the number with decimal place to an integer
value
document.write(Math.round(23.45)); //return 23
document.write(Math.round(123.65)); //return 124
ceil() It returns the smallest integer which is equal to our greater than the
given number
document.write(Math.ceil(23.35)); //return 24
document.write(Math. ceil (123.65)); //return 124
floor() It returns the largest integer which is equal to our lower than the
given number
document.write(Math.ceil(23.35)); //return 23
document.write(Math. ceil (123.65)); //return 123
getMonth() The month of the year as an integer from 0 to 11, where 0 is January
and 11 is December
indexOf(char) This function searches and (if found) returns the index number of
searched character or substring within the string if not found it return
-1
Var Str1=”Multimedia”;
Str1.indexOf(“t”); // it return 3
Q22 What is event handling? Which of the following two events will be required to
write a code to enlarge an image when the mouse pointer is over the image and
retains its original size when the mouse points anywhere else on the page?
OnMouseOver, OnMouseIn, OnMouseOut, OnMouseExit, OnClick,
OnMouseClick
Event handling refers to writing code that is executed to perform the processing in
response to occurance of an event.
Two events: onMouseOver, onMouseOut
Q24. Observe the code segment given below and answer the questions that follow:
<script language="JavaScript">
A=(10*3)%4
document.write(A)
B=40%3
document.write(B)
if(!(B>=A))
C=5
else C=10
document.write(C)
</script>
a) Name any one relational operator and one logical operator in the above code
Relational Operator >= Logical Operator !
b) Rewrite the statement: if (!(B>=A)) without using the ! operator. if (B<A)
Q25 Identify the error in the following codes and write the corrected script with the
correction underlined.
Also <script
a) give the output
lang="javascript"> b) <script language="javascript">
dim sum, var i;
a i=0
sum==0 for(i=1; i<-20; i++)
for(a=1; a<8, a++) print(i);
{ i==i+2;
sum=sum+a }
} <script>
document.write(sum + <BR>" + a)
</script>
An <script language="javascript"> An <script language="javascript">
s var sum=0,a s i=0
for(a=1; a<8; a++) for(i=1; i<=12; i++) {
{ document.write(i);
sum=sum+a i=i+2;
} }
document.write(sum + “<BR>" + a) </script>
</script>
Output Output
28 14710
8
c) <script language="javascript"> d) <script language="javascript">
var i, x; var
i=1 i=0,
x= x=0;
0 do
for(i==1; i<10; i*=2) while(i
document.text(x++) <10)
} if((i%2
response.write("<BR>" + i) )=0)
</script> { x=x+i
document.write(X + " " ); }
<script language="javascript"> <script language="javascript">
i++;
var i, x; var
}
i=1,x=0 i=0,
</script>
for(i=1; i<10; i*=2) x=0;
{ do{
document.write(x++) if(i%2
} ==0)
document.write("<BR>" + i) { x=x+i
</script> document.write(x + " " ); }
Output i++;
0123 } while(i<10)
16 </script>
Output
0 2 6 12 20
Q26. Give the output of the following code and rewrite the code using a for loop instead of
do..while loop without affecting the output:
<script language = JavaScript>
var prod, counter
prod = 1
counter = 1
do
{
prod = prod*counter
counter = counter+2
document.write(prod+", "+counter+"<BR>")
}
while (counter <=7)
Output:
1, 3
3, 5
15, 7
105, 9
Q27. Study the code given below and answer the questions that follow:
<SCRIPT LANGUAGE="JavaScript">
P=5
Q=30
do
{
P=P+6
document.write(P+" ")
}
while(P<=Q)
</SCRIPT>
(i) How many times the above WHILE loop gets executed?
5 Times
(ii) Convert the given DO WHILE loop to FOR loop without affecting the output.
<SCRIPT LANGUAGE="JavaScript">
P=5
Q=30
for(P=5;P<=35;P+=6)
{
document.write(P+" ")
}
</SCRIPT>
(iii) Give the output of the above code.
11 17 23 29 35
Q28. Change the following script to for loop to while loop while loop to for loop without
effecting the output: Give ouput also
a) var str = "INDIA"; b) var a, b, c, sum_even=0, sum_odd=0;
for(i=str.length; i>=1; i--) a=10, b=1;
{ while(b<=a)
for(a=0; a<i; a++) {
{ if(b%2==0)
document.write(str.charAt(a)); sum_even+=
} b
document.write("<br>"); else
} sum_odd+=b
b++;
}
document.write( sum_even +”<br>”);
An var str = "INDIA"; document.write(
An <script sum_odd +”<br>”);
language="javascript">
s i=str.length s sum_even=0,sum_odd=0
while( i>=1) a=10,b=1
{ for(b=1;b<=a;b++)
a=0; {
while(a<i) if(b%2==0)
{ document.write(str.charAt(a)); sum_even+=b
a++ } else
i--; sum_odd+=b
document.write("<br>"); }
} document.write(sum_even +"<br>")
Output document.write(sum_odd +"<br>")
INDIA </script>
INDI Output
IND 30
IN 25
I
Q30. Write the equivalent script for the following code using for loop without affecting
the output:
<script language="javascript"> Ans
ans=1 <script language="javascript">
count=2 ans=1
do { for(count=2;count<=10;count+=2)
ans=ans*count { ans=ans*count
count=count+2 }
}while (count<=10) document.write(ans)
document.write(ans) </script>
</script>
OUTPUT: 3840 OUTPUT: 3840
Write the JavaScript code to display the fee for the Dance Course as
600 for children aged 6-12
1000 for children aged 11-16
“Not Allowed” for any other age
On the click of the CALCULATE button. The user inputs the child’s age in the top text
box and the fee amount or the message “Not allowed” should be displayed in the
second text box.
<html> <head>
<script language = javascript>
function CalcFee()
{
age= parseInt(document.form1.age.value)
if (age>=6 && age<=12)
Fee = 600;
else if (age>=11 && age <=16)
Fee = 1000
else Fee = "Not Allowed"
document.form1.fee.value = Fee;
}
</script> </head>
<form name = form1>
<Pre>
<center>HOP AND DANCE CALCULATOR</center>
Write the JavaScript code to display the Stream for the Institute as
Science for percentage above 80
Commerce for percentage between 60 − 80
Humanities for percentage between 50 − 60
Not Eligible otherwise
on the click of the DISPLAY button.
The user inputs the child’s percentage in the top text box and the stream or the
message ‘‘Not Eligible’’ should be displayed in the second text box.
Write the JavaScript code to display appropriate message (as shown above) as to which
string is smaller on the click of the CHECK button.
<html>
<head>
<script language = javascript>
function Compare()
{
s1= document.form1.text1.value
s2= document.form1.text2.value
if (s1<s2)
alert("First string is smaller")
else if (s2<s1)
alert("Second string is smaller")
else alert("Strings are equal")
}
</script>
</head>
<form name = form1>
<Pre>
Enter the first string <input type = text name = text1>
Enter the second string <input type = text name = text2>
<input type = button value = Check onclick = Compare()>
</form>
</body>
</html>
Q34. Create a form that contains two text box options and radio button with two
options as shown below:
When the user clicks on any of the radio buttons, the
message should be displayed according to selected
Gender For example, if the First name entered by the user
is Neeraj and the Last Name entered by the user is Singh
the following message should be displayed according to the
selected gender:
Gender Message
Male Hello Mr. N. Singh. Welcome to our website.
Female Thank you Ms. N.Singh for visiting the website.
Write the HTML code for creating the form and the embedded JavaScript code for the click
event of the button.
<html>
<body>
<script language="javascript">
function hello()
{ fn = document.f1.fn.value;
ln = document.f1.ln.value;
alert("Hello Mr. "+fn[0]+". "+ln+". Welcome to our website.")
}
function bye()
{ fn = document.f1.fn.value;
ln = document.f1.ln.value;
alert("Thank you Ms. "+fn[0]+". "+ln+". for visiting the website.")
}
function msg()
{ gender = document.f1.gender.value;
if (gender == "m")
hello();
else if (gender == "f")
bye();
}
</script>
<form name = f1 action = js_qb.html>
First Name <input type = text name = fn> <P>
Last Name <input type = text name = ln> <P>
Gender <BR> <input type = radio name = gender value = m onclick = hello()>Male<BR>
<input type = radio name = gender value = f onclick = bye()>Female<P>
<input type = button value = "Show Me" onclick = msg()>
</form>
</body></html>
Q35. Create a form that contains two checkbox options and a textbox as shown
below. When the user clicks on any checkbox the selected options must be displayed
in the textbox. Write the HTML code for creating the form and the embedded
JavaScript code for the click events of the checkboxes.
<html>
<body>
<script language="javascript">
function show()
{
selection = "You have selected: ";
if (document.f1.Movies.checked)
selection += "Movies"
if (document.f1.Books.checked)
selection += " Books"
document.f1.t1.value = selection
}
</script>
<font size=4>
<B>The Check Box Control - Click on a check box</B>
<P>
<form name = f1>
Please select the categories that interest you <BR>
<input type = checkBox name = Movies onclick = show()>Movies<BR>
<input type = checkBox name = Books onclick = show()>Books<P>
<input type = text name = t1>
</form>
</body>
</html>
Q36. Create a Form and calculate Interest on basis of Interest Type Write JavaScript
code for calculate button
<head>
<script >
function Interest()
{ var n1 = parseFloat(document.f1.t1.value)
var n2 = parseFloat(document.f1.t2.value)
var n3 = parseFloat(document.f1.t3.value)
SI=(n1*n2*n3)/100
TA=n1+SI
CA=n1*(1+n2/100)*n3
CI=CA-n1
if(document.f1.r1[0].checked) {
document.f1.t4.value=SI
document.f1.t5.value=TA }
if(document.f1.r1[1].checked) {
document.f1.t4.value=CI
document.f1.t5.value=CA }
}
</script>
</head>
<body>
<form name="f1">
Interest Calculator<br>
Principle Amount<input type="text" name="t1" size=75> <br>
Rate(%) <input type="text" name="t2"> Time(Year) <input type="text" name="t3"><br>
Interest Type</Legend> <br>
Interest Rate <input type="radio" value="SI" name="r1"> Simple Interest
<input type="radio" value="CI" name="r1">Compund Interest <br>
Interest <input type="text" name="t4"> Total Amout<input type="text" name="t5"><br>
<input type="button" name="b1" value="Calculate" onclick="Interest()">
</form>
</body>
</html>