Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1K views

QB-JavaScript for WebApp

Java script

Uploaded by

Priya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

QB-JavaScript for WebApp

Java script

Uploaded by

Priya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Question Bank in JavaScript

1. What is JavaScript? Write any four characteristics of JavaScript.


2. Explain with an example, how JavaScript code can be embedded in an HTML document.
3. What is external script? When should we use external scripts? How to include an external script into an HTML
document?
4. What is a comment? Mention two types of comments available in JavaScript.
5. What are different types of operators available in JavaScript? Give some examples of each.
6. Evaluate the following expressions:
(i) 20+20 (ii) 20+”20” (iii) “20”+”20” (iv) 20+”Twenty”
(v) (20<10)?”T wenty”:”Ten”; (vi) (20>10)?”Twenty”:”Ten”;
7. What is a variable? How can a variable be declared in JavaScript?
8. What is the role of break in a switch statement?
9. What is the role of default in a switch statement?
10. Explain different ways in which events can be handled in an HTML code.
11. How is a value returned from a function? Explain with an example.
12. What is an array? Write JavaScript statement to declare an array of 5 objects.
13. What are events? When do the following events occur:
(i) onLoad, (ii) onUnload, (iii) onMouseOver, (iv) onMouseOut, (v) onSubmit, (vi) onClick
14. Which of the following option correctly matches the given events with the appropriate interface element?

(i) Button – OnChange, Text – OnClick, Image – OnMouseOver


(ii) Button – OnClick, Text – OnChange, Image – OnMouseOver
(iii) Button – OnMouseOver, Text – OnClick, Image – OnChange
15. 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
16. Name the event that occurs when the mouse pointer is brought over an element on a web page.
17. Name the built-in functions to be used in the following situations:
(i) To concatenate two arrays
(ii) To arrange the array elements in ascending order
(iii) To get a string of array elements separated by comma
(iv) To reverse the order of elements of an array
(v) To delete last element of an array
(vi) To delete first element of an array
(vii) To insert an element at the end of an array

1
(viii) To insert an element in the beginning of an array
(ix) To get a part of an array from one element to another.
(x) To delete some elements of an array and insert some other
18. Write the output of each of the following code segments:
(i) <script>
a=["red","blue","green"]
document.write(a,"<br>")
a.reverse()
document.write(a,"<br>")
a.sort()
document.write(a,"<br>")
</script>

(ii) <script>
a=["red","blue","green"]
a.push("yellow","white")
document.write(a,"<br>")
a.unshift("black","pink","magenta")
document.write(a,"<br>")
</script>

(iii) <script>
a=[1,"one",2,"two"]
clr=a.pop()
document.write(clr," - ",a,"<br>")
clr=a.shift()
document.write(clr," - ",a,"<br>")
</script>

(iv) <script>
a["black",'grey',"white"]
b=["yellow","purple","pink","golden"]
a=a.concat(b)
document.write(a,"<br>")
</script>

(v) <script>
a=new Array("Kirti","Shakti","Pragati","Jagriti")
c=a.slice(2,5)
document.write(c,"<br>")
a.splice(2,1,"x","y","K")
document.write(a,"<br>")
</script>

(vi) <script>
a=new Array("a",'b','c')
a[4]=4
a[6]=6
a.splice(2,4,3)
document.write(a,"<br>")
st=a.join("***")
document.write(st,"<br>")
</script>

2
19. Write the purpose of each of the following string methods and also give an example of each:
(i) charAt()
(ii) concat()
(iii) slice()
(iv) substring()
(v) split()
(vi) substr()
(vii) toLowerCase()
(viii) toUpperCase()

20. Write the output of the following code segment:


<script>
a=14.4
b=21
c=a/4
d=b/4
document.write(c," ",Math.round(c),"<br>")
document.write(d," ",Math.round(d),"<br>")
document.write(Math.random(),"<br>")
document.write(Math.abs(a-b),"<br>")
document.write(Math.abs(b-a),"<br>")
document.write(Math.pow(2,4),"<br>")
document.write(Math.sqrt(9),"<br>")
document.write(Math.max(5,2,3,6,1,9),"<br>")
document.write(Math.min(5,2,3,6,1,9),"<br>")
</script>

21. Differentiate between


(i) Null and undefined
(ii) Unary, binary, and ternary operators
(iii) confirm and prompt dialog boxes
(iv) confirm and alert dialog boxes
(v) Entry controlled loop and Exit controlled loop
(vi) while and do..while loops
(vii) for and for..in loops
(viii) Local and global variables
(ix) Actual and Formal parameters
(x) User defined functions and Built-in functions
(xi) join() and sort()
(xii) sort() and reverse()
(xiii) write() and writeln()
(xiv) Load and Unload events
22. Write the output of the following code:
(i) var i=10 (ii) j=10
j = (i++)+(++i)+(--i)+(i--) i = (++j)+(++j)+(j--)+(--j);
document.write(i+", "+j) document.write(i+", "+j)

3
(iii) i=10, j=15 (iv) var a=10;
i = (++i)+(i++); if (a%3>1)
j = (--j)-(j--) document.write(a++);
document.write(i+", "+j) else document.write(--a);
document.write(a)
(v) var i=10.5; (vi) var i=10, j=15, k = 20;
j = (i>10)?i++:++i; if (i>j)
document.write(i+", "+j) document.write(i+j+"<br>")
else if (i>k)
document.write(i+k+"<br>")
else if (k>j)
document.write(k-j+"<br>")
else document.write(i+j+k+"<br>")
(vii) var i=10, j=15, k = 20; (viii) var i=10, j=3;
if (i%j == 0) switch(i%j)
document.write(i+j+"<br>") {
else if (i%k) case 1: document.write("One");
document.write(i+k+"<br>") break;
else if (k%i==0) case 0: document.write("Zero")
document.write(k-i+"<br>") break;
else default:
document.write(i+j+k+"<br>") document.write("Remainder")
}
(ix) for (i=0; i<5; i++) (x) for (i=0; i<5; i+=2)
document.write(i+"$") document.write(i+"$")
(xi) for (i=0; i<5; i++) (xii) for (i=1; i<5; i++)
document.write(i++ +"$") document.write(i*2 +"$")
(xiii) for (i=1; i<5; i++) (xiv) var x=5, y = 12, z=2
if (i%2==0) for (i=x; i<=y; i++)
document.write(i/2 +"$") { if (i%z == 0)
else document.write(i*2+"<br>") document.write(z+"$")
else document.write(i+"$")
z++;
}
(xv) var x=5, y = 13 (xvi) var x=5, y = 13
while(y>=x) while(y>=x)
{ { for (i=x; i<=y; i++)
if (y%x==0) document.write(x+y+i+", ")
document.write(y/x+"$") document.write("<br>")
else document.write(y+x+"$") x++; y--;
x++; y--; }
}
(xvii) var x=5, y = 13 (xviii) x=10, y=20
do function f1()
{ k=y { document.write(x+y);
while (k%2==0) x+=y;
document.write(k++ + ", ") }

4
document.write("<br>") f1();
x++; y--; document.write(x+y)
}
while(y>=x)
(xix) function Change(x) (xx) x=10, y=20
{ for (i=x; i<=y; i+=3)
if (x%10==0 || x%3==0) adjust(i,x,y)
x+=5; document.write(i+", "+x+", "+y);
else x-=5; function adjust(a, b, c)
return x; {
} a+=b+c
k=12 b+=a-c
t=Change(k) c-=a+b
document.write(k+", "+t) document.write(a+b+c+"<br>")
}
23. Give the output of the following code segment:
(i)
var Names = new Array();
var Count
Count = 2
Names.length = Count;
Names[0]="Raj"
Names[1]="Simran"
Names[2]="Nisha"
Names.length = (Count + 2)
Names[3]="Maya"
Names[4]="Rohit"
document.write(Names[0]+Names[1]+Names[2])
document.write(Names[3]+Names[4])

(ii)
x = new Array();
for (i=0; i<5; i++)
x[i] = i*2;
for (i in x)
document.write(x[i]+", ")

(iii)
x = Array();
x[1]="red"; x[2]=18; x[4]=x[2]/4
document.write(x.length+"<br>")
for (i=0;i<6;i++)
document.write(x[i]+", ")
document.write("<br>")
for (i in x)
document.write(x[i])

(iv)
x = new Array(12, 6, 18, 24, 3);
x.reverse();

5
for (i in x)
document.write(x[i]+", ")
document.write("<br>")
x.sort();
for (i in x)
document.write(x[i]+"*")

(v)
x = new Array(12, 6, 18, 24, 3);
x.reverse().sort();
for (i in x)
document.write(x[i]+", ")
document.write("<br>")
x.sort().reverse()
for (i in x)
document.write(x[i]+"*")

(vi)
x = Array("CS", 83, "MWT", 67);
document.write(x.length+"<br>");
document.write(x.join()+"<br>")
document.write(x.join("-")+"<br>")
document.write(x.toString()+"<br>")

(vii)
x = "MultiMedia"; y = "067";
document.write(x.length+"<br>");
document.write(x.charAt(y.length)+"<br>")
document.write(x.concat(y)+"<br>")
document.write(y.concat(x)+"<br>")

(viii)
x = "Indian"; y = "soil";
document.write(x.slice(3,4)+"<br>");
document.write(y.split()+"<br>")
document.write(y.split('o')+"<br>")
document.write(x.substr(4,3)+"<br>")
document.write(x.substring(5,3)+"<br>")
document.write(x.toLowerCase()+"<br>")
document.write(y.toUpperCase()+"<br>")

24. Write the output of the following code:


var result = 0
for (var i = 1; i<=5; i++)
result = result + second(4)
document.write(result+"")
function second(num)
{ t = num*5; num = num + 1; return t; }

6
25. Give the output of the following code and rewrite the code using a for loop instead of do..while loop without
affecting the output:
var prod, counter
prod = 1
counter = 1
do
{ prod = prod*counter
counter = counter+2
document.write(prod+", "+counter+"<BR>")
}
while (counter <=7)

26. Predict the output of the following code:


<script language = JavaScript>
function change (a, b)
{ a=a+a
b = b*b
document.write(a+", "+b+"<BR>")
}
c=3; d=10;
e=5; f=20;
change(c,d);
change(e,f)
</script>
27. 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?
(ii) Convert the given DO WHILE loop to FOR loop without affecting the output.
(iii) Give the output of the above code.

28. Study the code given below and answer the questions that follow:
<SCRIPT LANGUAGE="JavaScript">
Q=30
for (P = 5; P<=Q; P+=6)
document.write(P+"<BR>")
</SCRIPT>
(i) How many times the above for loop gets executed?
(ii) Convert the given for loop to do..while loop without affecting the output.
(iii) Give the output of the above code.

29. Observe the code segment given below and answer the questions that follow:
<script language = JavaScript>

7
A = (10**3) % 4/2
B = 40%3
if (!(B>=A))
C = 5;
else C =10;
<script>
a) Name any one relational operator and one logical operator in the above code segment.
b) Rewrite the statement: if (!(B>=A)) without using the ! operator.

30. Rewrite the following code after removing errors with each correction underlined :
(i) x=5
Do
if x%2=0
then document.Write x
x++
while x<=10
(ii) var a = 5
document.print(a)
if a>2
a =+ 5;
else a =* 2
alert a
(iii) k=5
while k=>2
{document.write "k"
k--
}
document.write (<hr>)
(iv) a = 17
b=a%5
switch b
{
0: alert(0);
Break;
default: alert("default")
1: alert("1");
Break;
}
(v) a=17; b=a%5
do while b>0
{
var c=10;
a=+b;
b--;
Document.write(a+b);
c=a+b;
}
document.write(c)
(vi) for(i=1, i<10,)
if i%2 =< 1

8
i++;
else ++i;
(vii) func first{a, b}
(a>b)?return a:return b;

x=10
document.show(first(x,20))

(viii) func second(a+b)


{
a++;
++b;
Return a+b
}
x=10
second(x)
second(x,x)
second(10+x)

31. Rewrite the following code using if..else statement:


switch(choice)
{ case 1: document.write("Monday");
break;
case 2: document.write("Tuesday");
break;
case 3: document.write("Wednesday");
break;
default:document.write("Sunday");
}
32. Write JavaScript code to input first name, middle name, and last name of a person and display the
abbreviated name of the person on screen. For example, if the three values are Ram, Mohan, and Pandey
respectively, then the output should be R. M. Pandey.
33. Write JavaScript code to input the length and breadth of a rectangle and display its area and perimeter on
screen.
34. Write JavaScript code to input the values of principal, rate, and time and calculate simple interest. Display the
simple interest in an alert box.
35. Write JavaScript code to input a number and check whether it is even or odd.
36. Write JavaScript code to input three numbers and find the smallest.
37. Write JavaScript code to input three numbers and find the largest.
38. Write JavaScript code to input the aggregate marks of a student and calculate the grade as per the following
criteria:
Marks Grade
>=91 A
81-90 B
71-80 C
61-70 D
<61 E
39. Write JavaScript code to display first 10 natural numbers.
40. Write JavaScript code to find the sum of first 10 natural numbers.
41. Write JavaScript code to display first 10 even natural numbers.
42. Write JavaScript code to find the sum of first 10 odd natural numbers.
43. Write the code which generates the following output:
Hello
Hell
Hel
He
H

9
44. Write a user-defined function that takes two integers as parameters and returns their sum.
45. Write a user-defined function that takes three number as parameters and returns the largest of the three
numbers.
46. Write a user-defined function which takes a string as a parameter and displays the string in reverse order.
47. Create a web page which, when loaded, displays a message box with the message “Good Morning” if the
page is loaded before 12 o’ clock, and “Good Day” otherwise. When the page is unloaded, it should display
the message “Bye bye”.
48. Write the HTML code to generate the following form:

Write the JavaScript code to display the fee for the Music 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.
49. Write the HTML code to generate the following form:

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.
50.

Write the JavaScript code to display appropriate message (as shown above) as to which string is smaller on
the click of the CHECK button.

10
51. Write code to create a form that contains two text box options and radio button with two options as shown
below:

When the user clicks on ‘Show Me’ button, 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.

52. Write HTML document to create the following form:

Use JavaScript to find out the division secured by a student on the basis of the % of marks obtained in the
examination. The student is failed if the marks are below 33%, he secures a III division if the marks are 33%
onwards but below 50%, a II division if he secures 50% onwards but below 60%, a I division if he secures
60% onwards but below 75% and a Distinction if the score is 75% and onwards.

11
Answers

1. JavaScript is a server side scripting language which is used to create interactive web pages.
Four characteristics of JavaScript:
 JavaScript is the default scripting language of HTML
 It is a client-side scripting language.
 JavaScript is case-sensitive
 JavaScript code can be embedded in HTML code or it can be kept in a separate file.
2. There are two ways to embed JavaScript code in HTML:
(i) As statements and functions using the <script>..</script> element.
Example:
<body>
<script>
Document.write("Hello")
</script>
<hr>
</body>
(ii) Using event handler as HTML tag attribute
Example:
<body onload='alert("Hello")' onunload='alert("Bye bye")'>
3. An external script is a text file containing script only. A client-side external script is a file with
extension .js and has code written in JavaScript.
We should use external scripts when the script is very long and/or when the script has to be used by
multiple HTML files.
An external script is included in an HTML document by using src attribute of the <script> tag.
For example, if the external script file external.js has to be included in an HTML file, then in the
HTML file we have to write <script src=external.js>
4. A comment is a part of code that is not interpreted by the browser. The browser ignores all the
comments appearing in the code.
Two types of comments available in JavaScript are:
(i) Single line comment: A single line comment starts with // and extends till the end of the
current line
(ii) Multi line comment: A multi line comment starts with /* and ends with */. All the part of
code included between /* and */ is taken as a comment. It may be a part of a line, a
complete line, or it may run into multiple lines also.
5. Different types of operator available in JavaScript are:
1. Arithmetic (or Algebraic) operators. Example: +, -, *, /, %
2. Assignment operators. Example: =
3. Relational (or Comparison) operators. Example: >, >=, <, <=, ==, !=

12
4. Logical operators. Example: !, &&, II
5. String operator. Example: + (Concatenation)
6. Conditional Operator. Example: ?:
7. Shorthand operators. Example: +=, -=, /=, *=
6. (i) 40 (ii) 2020 (iii) 2020 (iv) 20Twenty (v) "Ten" (vi)
"Twenty"
7. A variable is a name given to a memory location where the script can store a value.
In JavaScript a variable can be declared in two ways:
(i) By declaring it with keyword var. Example: var a;
(ii) By assigning a value to a variable. An undeclared variable gets automatically declared
when it is assigned value for the first time. Example: b=10;
8. The break statement terminates the switch statement and takes the control to the next statement
in the code.
9. The default clause in switch statement is used to handle the case when none of the mentioned
cases (using case clause) is met. The code specified in the default clause is executed when no
match is found in the specified case clauses.
10. There are two ways of event handling in HTML:
(i) Using event handler as HTML tag attribute.
Example:
<body onload='alert("Hello")' onunload='alert("Bye bye")'>
(ii) Writing a JavaScript function and calling the function on the occurrence of desired
event.
Example:
<html>
<head>
<script language=JavaScript>
function Hello()
{ alert("Hi") }
</script> </head>
<body>
<input type=button onclick=Hello()>
</body> </html>
11. A value is returned from a function using return statement.
Example:
function Sum(a, b)
{ return a+b }
12. An array is a type of variable that groups a series of values in a single variable.

13
JavaScript statement to declare an array of 5 objects: var arr = new Array(5);
13. Events are things that happen, usually user actions that are associated with an object. Occurance
of given events:
(i) onLoad – occurs when a page loads in a browser
(ii) onUnload – occurs just before the user exits a page
(iii) onMouseOver – occurs when mouse pointer is taken over an object
(iv) onMouseOut – occurs when mouse pointer is taken out of an object
(v) onSubmit – occurs when a form is submitted
(vi) onClick – occurs when an object is clicked
14. (ii) Button – OnClick, Text – OnChange, Image – OnMouseOver
15. Event handling refers to writing code that is executed to perform the processing in response to
occurrence of an event.
Two events: MouseOver, MouseOut
16. OnMouseOver event
17. (i) concat(), (ii) sort(), (iii) join(), (iv) reverse(), (v) pop(),
(vi) shift(), (vii) push(), (viii) unshift(), (ix) slice(), (x) splice()
18. (i) red,blue,green
green,blue,red
blue,green,red

(ii) red,blue,green,yellow,white
black,pink,magenta,red,blue,green,yellow,white

(iii) two - 1,one,2


1 - one,2

(iv) one,2,yellow,purple,pink,golden

(v) Pragati,Jagriti
Kirti,Shakti,x,y,K,Jagriti

(vi) a,b,3,6
a***b***3***6
19.
Method Purpose Example
charAt() Returns the character from a specified "Faips".charAt(3) returns
position in a string. "p".
concat() Joins a string with another and returns the "Faips".concat("DPS")
resultant string. The original strings remain returns "FaipsDPS".
unaffected.
slice()/ Returns substring between two indices of "Faips".slice(1,4) returns
substring() a given string. The substring starts at the "aip".
first index and ends before the second
index. The original string remains
unaffected.
split() Splits a string into an array of substrings "vulture".split("u") returns
separated by given character in the string. an array ["v","lt","re"]
The original strings remain unaffected.

14
substr() Returns a substring containing a number "Faips".slice(1,4) returns
of characters starting from a given index "aips".
number of a given string. The original
strings remain unaffected.
toLowerCase() Returns the given string in lowercase. The "Faips".toLowerCase()
original strings remain unaffected. returns "faips".
toUpperCase() Returns the given string in uppercase. The "Faips".toUpperCase()
original strings remain unaffected. returns "FAIPS”.
20. 3.6 4
5.25 5
A random number in the range 0 to less than 1.
6.6
6.6
16
3
9
1
21. (i)
null undefined
null represents no value at all, not even 0 or undefined means a variable has been declared but
blank. has not been assigned any value yet.
null is an explicitly assigned value undefined is an implicitly assigned value
Example: Example:
var a var a
// a has undefined value at this // a has undefined value at this
point point
a=null a=5
// a has a null value // Now a is not undefined

(ii)
Unary operators Binary operators Ternary Operators
operate on one operand Operate on two operands Operate on three operands
Example: ++, -- Example: *, /, % Example: ?:

(iii)
confirm prompt
It has no input box It has an input box
It is used to get some confirmation from the It is used to get some textual input from the user.
user
Example: Example:
confirm("Sure to exit?") prompt("Enter your name: ")

(iv)

15
confirm alert
It displays a message and two buttons: OK and It displays a message and only one button: OK
Cancel
It is used to get some confirmation from the It is used to show some message to the user.
user
Example: Example:
confirm("Sure to exit?") alert("Closing in 3 minutes"

(v)
Entry controlled loop Exit controlled loop
Looping condition is specified in the beginning of
Looping condition is specified at the end of the loop.
the loop.

Loop does not execute even once if the Loop executes at least once irrespective of the initial
condition is false in the beginning. value of the condition.

Examples: for loop and while loop Example: do..while loop

(vi)
While do..while
while is an entry-controlled loop do..while is an exit controlled loop.
Loop does not execute even once if the Loop executes at least once irrespective of the initial
condition is false in the beginning. value of the condition.

(vii)
for for..in
Can be used with arrays or any other value Can be used only with arrays
Initialization, looping condition, and update have Initialization, looping condition, and update are taken
to be specified explicitly. automatically.
Example: Example:
for (i=1; i<=5; i++) a = Array(4,2,8,7)
document.write(i); for (i in a)
document.write(a[i])

(viii)
Local Variable Global variable
A local variable is defined inside a function using A global variable is defined by assigning a value to it
the keyword var. without using the keyword var.
A local variable can be used only in the function A global variable can be accessed in any block of the
in which it is defined. script on the page.
Example:
<script language=JavaScript>
var a = 10 //a is a global variable
b = 20 //b is a global variable
function f1()

16
{
var c = 30 //c is a local variable
d=40 //d is a global variable
alert(a+", "+b+", "+c+", "+d)
}
f1();
</script>
<script language=JavaScript>
alert(a)
alert(b)
//alert(c) - c is not defined here as it was a local variable in f1().
alert(d)
</script>

(ix)
Actual parameter Formal parameter
These are the parameters used in function These are the parameter used in function definition.
call/invocation.
Example:
function Sum(a, b) // a and b are formal parameters
{ return a+b }
C=Sum(4,2) //4 and 2 are actual parameters

(x)
User defined function Built-in function
These are the functions written by the user in a These are the functions pre-defined or built-in into
script. the JavaScript interpreter.
These can be used only in the scripts in which These can be used in any script.
they are defined.
Example:
function Sum(a, b) // Sum() is a user-defined function
{ return a+b }
x = parseInt(prompt(“Enter a number”))
//parseInt() and prompt() are built-in functions
C=Sum(x,2)

(xi)
join() sort()
Joins all elements of the array into a Sorts elements of the array in ascending order.
string. The array elements are separated
by the specified delimiter.
Accepts the delimiter as a parameter. Default Accepts no parameter.
value of the delimiter is comma (,).
Example:

17
a = Array("A", "C", "B")
a.sort(); //sorts array a in ascending order
document.write(a); //display "A,B,C"
b = a.join(" and ");
document.write(b); //displays "A and B and C"

(xii)
sort() reverse()
Sorts elements of the array in ascending order. Reverses the elements of the array
Example: Example:
a = Array("A", "C", "B") a = Array("A", "C", "B")
a.sort(); a.reverse();
document.write(a); //display document.write(a); //display
"A,B,C" "B,C,A"

(xiii)
write() writeln()
It does not add a new line character It adds a new line character (equivalent to <br>) at the end of
(equivalent to <br>) at the end of output.
output.
It does not require any other tag. It needs to be put between <pre>..</pre> tags. If not done
so, writeln() does not put new line character at the end of
output.

(xiv)
Load Unload
Load event occurs when a window is loaded. Unload event occurs when a window is closed.
22. (i) 10, 44
(ii) 45, 10
(iii) 22, 0
(iv) 99
(v) 11.5, 10.5
(vi) 5
(vii) 30
(viii) One
(ix) 0$1$2$3$4$
(x) 0$2$4$
(xi) 0$2$4$
(xii) 2$4$6$8$
(xiii) 2
1$6
2$

18
(xiv) 5$3$7$8$9$10$11$12$
(xv) 18$2$18$18$1$
(xvi) 23, 24, 25, 26, 27, 28, 29, 30, 31,
24, 25, 26, 27, 28, 29, 30,
25, 26, 27, 28, 29,
26, 27, 28,
27,
(xvii) 12,

10,
(xviii) 3050
(xix) 12, 17
(xx) 20
20
20
20
22, 10, 20
23. (i) RajSimranNishaMayaRohit
(ii) 0, 2, 4, 6, 8,
(iii) 5
undefined, red, 18, undefined, 4.5, undefined,
red184.5
(iv) 3, 24, 18, 6, 12,
12*18*24*3*6*
(v) 12, 18, 24, 3, 6,
6*3*24*18*12*
(vi) 4
CS,83,MWT,67
CS-83-MWT-67
CS,83,MWT,67
(vii) 10
t
MultiMedia067
067MultiMedia
(viii) i
soil
s,il
an
ia
indian
SOIL
24. 100

19
25. Output
1, 3
3, 5
15, 7
105, 9

Code using for loop


<script language = JavaScript>
var prod, counter
prod = 1
for (counter = 1; counter<=7; )
{
prod = prod*counter
counter = counter+2
document.write(prod+", "+counter+"<BR>")
}
</script>
26. 6, 100
10, 400
27. (i) 5 Times
(ii) Q=30;
for (P=5; P<=Q; P+=6)
document.write((P+6)+" ")
(iii) 11 17 23 29 35
28. (i) 5 Times
(ii) Q=30
P=5
do
{
document.write(P+"<BR>")
P+=6;
}
while (P<=Q);
(iii) 5
11
17
23
29
29. c) Relational operator: >= Logical operator: !
d) if (B<A)
30. (i)
x=5
do
{
if (x%2==0)
then document.write(x)
x++
}
while (x<=10)

20
(ii)
var a = 5
document.write(a)
if (a>2)
a =+ 5;
else a *= 2
alert (a)
(iii)
k=5
while (k>=2) //2 corrections
{document.write ("k")
k--
}
document.write ("<hr>")
(iv)
a = 17
b=a%5
switch(b)
{
case 0: alert(0);
break;
default: alert("default")
case 1: alert("1");
break;
(v)
a=17
b=a%5
do while b>0
{ var c=10;
a=+b;
b--;
document.write(a+b);
c=a+b;
}
while (b>0)
document.write(c)
(vi)
for(i=1; i<10;)
if (i%2 >= 1) // 2 corrections
i++;
else ++i;
(vii)
function first(a, b)
{
return (a>b)?a:b;
}

x=10
document.write(first(x,20))

21
(viii)
function second(a, b)
{
a++;
++b;
return a+b
}

x=10
document.write(second(x,12))
alert(second(x,x))
alert(second(x,x))
31. if (choice == 1)
document.write("Monday");
else if (choice == 2)
document.write("Tuseday");
else if (choice == 3)
document.write("Wednesday");
else document.write("Sunday");
32. <script language=JavaScript>
fname = prompt("Enter first name: ")
mname = prompt("Enter middle name: ")
lname = prompt("Enter last name: ")
document.write(fname[0]+"."+mname[0]+"."+lname)
</script>
33. <script language=JavaScript>
len = parseFloat(prompt("Enter length: "))
br = parseFloat(prompt("Enter breadth: "))
area = len*br
document.write("Area = "+area)
document.write("<hr>")
</script>
34. <script language=JavaScript>
p = parseFloat(prompt("Enter Principal: "))
r = parseFloat(prompt("Enter Rate: "))
t = parseFloat(prompt("Enter Time: "))
interest = p*r*t/100
document.write("Simple Interest = "+interest)
document.write("<hr>")
</script>
35. n = parseInt(prompt("Enter an integer: "))
if (n%2 == 0)
document.write("Even");
else document.write("Odd")
36. <script>
a = parseFloat(prompt("Enter first number: "))
b = parseFloat(prompt("Enter second number: "))
c = parseFloat(prompt("Enter third number: "))
document.write(Math.min(a,b,c))

22
</script>
37. <script>
a = parseFloat(prompt("Enter first number: "))
b = parseFloat(prompt("Enter second number: "))
c = parseFloat(prompt("Enter third number: "))
document.write(Math.max(a,b,c))
</script>
38. marks = parseInt(prompt("Enter aggregate marks: "))
if (marks >= 91)
grade="A";
else if (marks >= 81)
grade="B";
else if (marks >= 71)
grade="C";
else if (marks >= 61)
grade="D";
else grade="E";
document.write("Your grade is: "+grade)
39. for (i=1;i<=10;i++)
document.write(i+" ")
OR
i = 1;
while (i<=10)
{ document.write(i+" ")
i++;
}
OR
i=1
do
{ document.write(i+" ")
i++;
}
while (i<=10)
40. sum=0
for (i=1;i<=10;i++)
sum += i;
document.write("Sum = "+sum)
OR
sum = 0
i = 1;
while (i<=10)
{ sum += i;
i++;
}
document.write("Sum = "+sum)
OR
sum=0
i=1
do
{

23
sum+=i;
i++;
}
while (i<=10)
document.write("Sum = "+sum)

41. sum=0, even=2


for (i=1;i<=10;i++)
{ sum += even;
even += 2
}
document.write("Sum = "+sum)
OR
sum = 0, even=2
i = 1;
while (i<=10)
{ sum += even;
even+=2
i++;
}
document.write("Sum = "+sum)
OR
sum=0, even=2
i=1
do
{ sum+=even;
even+=2
i++;
}
while (i<=10)
document.write("Sum = "+sum)
42. sum=0, odd=1
for (i=1;i<=10;i++)
{ sum += odd;
odd +=2;
}
document.write("Sum = "+sum)
OR
sum = 0, odd=1
i = 1;
while (i<=10)
{ sum += odd;
odd +=2;
i++;
}
document.write("Sum = "+sum)
OR
sum=0; i=1;
do
{ sum += odd;

24
odd +=2;
i++;
}
while (i<=10)
document.write("Sum = "+sum)
43. <script>
var H = "Hello";
var l = H.length;
for (i=l; i>=1; i--)
document.write(H.substr(0,i)+"<BR>")
</script>
44. function sum(a, b)
{ return (a+b)}
45. function largest(a, b, c)
{
return(Math.max(a,b,c))
}
46. function strrev(a)
{
l=a.length
for (i=l-1; i>=0; i--)
document.write(a[i]);
}
47. <script>
function greetings()
{
d = new Date()
hh = d.getHours()
if (hh<=12)
alert("Good morning")
else alert("Good day")
}
</script>
<body onload=greetings() onunload='alert("Bye bye")'>
</script>
</body>
48. <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>

25
<form name = form1>
<h2 align = center>Music Fee Calculator</h2>
<pre>
Enter Child's Age <input type = text name = age>
Fee Amount <input type = text name = fee>
<input type = button value = Calculate onclick = CalcFee()>
</form> </body> </html>
49. <head>
</head>
<body>
<script language="javascript">
function stream()
{
per= parseInt(document.f1.per.value);
if (per>80)
document.f1.str.value = "Science";
else if (per>60 && per<=80)
document.f1.str.value = "Commerce";
else if (per>50 && per <=60)
document.f1.str.value = "Humanities";
else document.f1.str.value = "Not Eligible";
}
</script>
<font size=4>
<center>EduSmart Stream Choice</center>
<form name = f1 action = js_qb.html>
<pre>
Enter Child's Percentage <input type = text name = per>

Stream <input type = text name = str>

<input type = button value = "DISPLAY" onclick = stream()>

</form>
</body>
</html>
50. <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>

26
<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>
51. <html>
<head>
</head>
<body>
<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>Male
<BR>
<input type = radio name = gender value = f>Female
<P>
<input type = button value = "Show Me" onclick = msg()>
</form>
<script language="javascript">

function msg()
{
fn = f1.fn.value;
ln = f1.ln.value;
gender = f1.gender.value;
if (gender == "m")
alert("Hello Mr. "+fn[0]+". "+ln+". Welcome to our website.")
else if (gender == "f")
alert("Thank you Ms. "+fn[0]+". "+ln+". for visiting the
website.")
}
</script>
</body>
</html>
52. <html>
<head>
<script language = javascript>
function CalcGrade()
{
per = parseFloat(form1.percentage.value)
if (per<33)
grade = "Fail";
else if (per<50)
grade = "IIIrd Division";
else if (per<60)

27
grade = "IInd Division";
else if (per < 75)
grade = "Ist Division";
else
grade = "Distinction";
if (per<0 || per>100)
{
grade = ""
alert("Marks out of range")
}
form1.grade.value = grade;
}
</script>
</head>
<body>
<form name = form1>
<Pre>
<center> Calculate Your Divition</center>
Enter your percentage <input type = text name = percentage>
GRADE <input type = text name = grade>
<input type = button value = Calculate onclick = CalcGrade()>
</form>
</body>
</html>

28

You might also like