Client Side Scripting Unit-2
Client Side Scripting Unit-2
W-19
Q.1. (d) Write a Java script that initializes an array called flowers with the names of 3 flowers. The script then
Q.1. (e) Write Java script to call function from HTML. (2M)
<html>
<body>
<script>
var l=parseInt(prompt("Enter length="));
var b=parseInt(prompt("Enter breadth="));
area();
function area()
{
var a=l*b;
alert("Area="+a);
}
</script>
</body>
</html>
Q.2. (c) Write a Java script program which computes, the average marks of the following students then, this average
1
Unit-2 Array, Function and String Vikas Patil
<70 D
<80 C
<90 B
<100 A
<html>
<head>
<title>Average marks</title>
<script>
var students =
[['Sumit',80],['Kalpesh',77],['Amit',88],['Tejas',93],['Abhishek',65]];
var Avgmarks=0;
for(var i=0;i<students.length;i++){
Avgmarks+=students[i][1];
}
var avg=(Avgmarks/students.length);
document.write("Average marks : "+avg);
document.write("<br>");
if(avg<=60){
document.write("Grade : E");
}else if(avg<=70){
document.write("Grade : D");
}else if(avg<=80){
document.write("Grade : C");
}else if(avg<=90){
document.write("Grade : B");
}else if(avg<=100){
document.write("Grade : A");
}else{
document.write("Invalid");
}
</script>
</head>
<body></body>
</html>
Q.3. (a) Differentiate between concat () and join () methods of array object. (4M)
Q.3. (b) Write a Java script that will replace following specified value with another value in a string. String = “ I will
2
Unit-2 Array, Function and String Vikas Patil
Q.3. (c) Write a Java script code to display 5 elements of array in sorted order. (4M)
<html>
<head>
<title>Sort 5 elements in array</title>
<script>
var car=["Thar","Urus","Supra","Mustang","LaFerrari"];
document.write("Original Array : "+car+"<br>");
car.sort();
document.write("Sorted Array : "+car+"<br>");
</script>
</head>
</html>
W-22
Q.1. (d) Write a program using sort method of array object. (2M)
<html>
<head>
<title>Sort 5 elements in array</title>
<script>
var car=["Thar","Urus","Supra","Mustang","LaFerrari"];
document.write("Original Array : "+car+"<br>");
car.sort();
document.write("Sorted Array : "+car+"<br>");
</script>
</head>
</html>
Q.2. (a) Write a JavaScript program that will display current date in DD/MM/YYYY format. (4M)
<html>
<head>
<title>Display current date in DD/MM/YYYY format. </title>
</head>
<body>
<script>
var d= new Date();
var currentDate=d.getDate()+"/"+d.getMonth()+"/"+d.getFullYear();
document.write(currentDate);
</script>
</body>
</html>
Q.2. (b) Write a JavaScript program that will remove the duplicate element from an array. (4M)
<html>
<body>
<script>
let arr = ["scale", "happy", "strength", "peace", "happy", "happy"];
document.write(arr+"<br>");
function removeDuplicates(arr) {
let unique = [];
for (i = 0; i < arr.length; i++) {
if (unique.indexOf(arr[i]) === -1)
{
unique.push(arr[i]);
3
Unit-2 Array, Function and String Vikas Patil
}
}
return unique;
}
document.write(removeDuplicates(arr));
</script>
</body>
</html>
Q.2. (c) Write a JavaScript program that will display list of student in ascending order according to the marks & calculate
the average performance of the class. (4M)
Student Name Marks
Amit 70
Sumit 78
Abhishek 71
<html>
<body>
<script>
var students = [["Amit", 70],["Sumit", 78],["Abhishek", 71],];
var Avgmarks = 0;
for (var i = 0; i < students.length; i++) {
Avgmarks += students[i][1];
}
var avg = Avgmarks / students.length;
document.write("Average grade: " + Avgmarks / students.length);
document.write("<br><br>");
students.sort(function(a, b) {
return a[1] - b[1];
});
Q.2. (d) Write and explain a string functions for converting string to number and number to string. (4M)
<html>
<body>
<script>
var a=parseInt(prompt("Enter A number : "));
document.write(a+"<br>");
var b = a.toString();
document.write(b);
</script>
</body>
</html>
4
Unit-2 Array, Function and String Vikas Patil
Q.3. (a) Differentiate between concat( ) & join( ) methods of array object. (4M)
Q.3. (b) Write a JavaScript function to check the first character of a string is uppercase or not. (4M)
<html>
<head>
<title>Check 1st alphabet Uppercase or not</title>
<script>
function checkUpperCase(){
var str=prompt("Enter a string ");
var regUpper=/^[A-Z]/;
if(regUpper.test(str)){
alert ("Is in upper case");
}else{
alert ("Not in uper case");
}
}
checkUpperCase();
</script>
</head>
</html>
Q.3. (c) Write a JavaScript function to merge two array & removes all duplicate values. (4M)
<html>
<body>
<script>
function arrmeth(arr1, arr2) {
var mergedArray = arr1.concat(arr2);
return uniqueArray;
}
S-22
Q.2. (b) Differentiate between concat() and join() methods of array object. (4M)
Q.4. (b) Write a javascript function that accepts a string as a parameter and find the length of the string. (4M)
<html><body>
<script>
function len(text){
return(text.length);
}
document.write("Lengty Of String : "+len("Vikas"));
</script>
</body>
</html>
5
Unit-2 Array, Function and String Vikas Patil
Q.5. (b) Develope javascript to convert the given character to unicode and vice-versa. (6M)
<html>
<body>
<script>
var num=70;
var char=String.fromCharCode(num);
document.write("Character at unicode(70) : "+char+"<br>");
var char1="H";
var unicode=char1.charCodeAt();
document.write("Unicode of Character(H) : "+unicode)
</script>
</body>
</html>
Q.6. (c) Write a javascript function to generate Fibonacci series till user defined limit. (6M)
<html>
<body>
<script>
function fibonacci(num)
{
var x = 0,y = 1,z;
document.write(x);
document.write("<br>"+y);
for (var i = 2; i < num; i++)
{
z = x + y;
x = y;
y = z;
document.write("<br>"+y);
}
}
var num = parseInt(prompt('Enter the number of terms: '));
fibonacci(num);
</script>
</body>
</html>
S-23
Q.1. (d) Write a JavaScript that initializes an array called Colors with the names of 3 Colors and display the array
elements. (2M)
Q.1. (e) Explain calling a function with arguments in JavaScript with example. (2M)
<html>
<body>
<script>
var l=parseInt(prompt("Enter length="));
var b=parseInt(prompt("Enter breadth="));
area();
function area()
6
Unit-2 Array, Function and String Vikas Patil
{
var a=l*b;
alert("Area="+a);
}
</script>
</body>
</html>
Q.2. (d) Write the use of charCodeAt() and from CharCode() method with syntax and example. (4M)
Q.3. (a) Differentiate between push() and join() method of array object with respect to use, syntax, return value and
example. (4M)
Q.3. (b) Write JavaScript code to perform following operations on string. (Use split() method) (4M)
Display output as
Q.3. (c) Explain splice() method of array object with syntax and example. (4M)
W-23
Q.1. (d) Differentiate between shift() and push() methods of an Array object. (2M)
Q.1. (e) State the meaning of “Defining a function”. Explain with the help of an example. (2M)
Q.2. (d) Write an HTML script that accepts Amount, Rate of Interest and Period from user. When user submits the
information a JavaScript function must calculate and display simple interest in a message box. (Use formula
Q.3. (c) Differentiate between substring() and substr() method of a string class. Give suitable example of each. (4M)
7
Unit-2 Array, Function and String Vikas Patil
S-24
Q.1. (d) Write a JavaScript program to display the elements of array in ascending and descending order. (2M)
<html>
<head>
<title>Sort 5 elements in array</title>
<script>
var car=["Thar","Urus","Supra","Mustang","LaFerrari"];
document.write("Original Array : "+car+"<br>");
car.sort();
document.write("Sorted Array : "+car+"<br>");
car.reverse();
document.write("Reverse Sort : "+car+"<br>")
</script>
</head>
</html>
Q.1. (e) Give syntax of and explain function in JavaScript with suitable example. (2M)
i) charCodeAt( )
ii) fromCharCode ( )
Q.3. (c) Explain how to add and sort elements in array with suitable example. (4M)