Você está na página 1de 11

Nehru Nagar, Puttur - 574 203

PROGRAMMING THE
WEB
JavaScript (Chapter 4) solved Exercises

PREPARED BY
DIVYA
Asst. Professor, ISE dept.
VCET, Puttur
Divya.shettigar.hp@gmail.com
TEXT BOOK: PROGRAMMING THE WORLD WIDE WEB Robert W Sebesta, 4th
Edition, Pearson Education, 2008
Divya, Asst. Prof. ISE dept. VCET puttur

Page 1

4.1
Table of numbers from 5 to 15 and their squares and cubes using alert.
<html>
<head>
<script language="JavaScript">
<!--//
function sq()
{
var number, square, cube;
var sq1;
sq1="number ------------ square ------------- cube \n";

for (number = 5; number < 16; number++) {


square = number * number;
cube = number * square;
sq1 = sq1+number + "--------------------- " + square + "-------------------- " + cube + "\n";
}
alert(sq1);
}
//-->
</script>
</head>
<body>
<form>
<input type="button" value="sq" onclick="sq();"/>
</form>
</body>
</html>

4.2
First 20 Fibonacci numbers which is defined as in the following sequences.
1,1,2,3,....
<html>
<head>
<script language="JavaScript">
<!--//
function sq()
{
var f1= 1, f2 = 1, next, count;
document.write("First 20 Fibonacci Numbers <br/><br/>");
document.write("1, 1");
for (count = 3; count <= 20; count++) {
f3 = f1 + f2;
document.write(","+f3);
f1 = f2;
Divya, Asst. Prof. ISE dept. VCET puttur

Page 2

f2 = f3;
}
}
//-->
</script>
</head>
<body>
<form>
<input type="button" value="sq" onclick="sq();"/>
</form>
</body>
</html>

4.3
Largest of three numbers using predefined Math.max
<html>
<head>
<script language="JavaScript">
<!--//
function sq()
{
var a=prompt( "Enter first number " , "" );
a=parseInt(a);
var b=prompt( "Enter first number " , "" );
b=parseInt(b);
var c=prompt( "Enter first number " , "" );
b=parseInt(b);
var max=Math.max(a,b,c);
alert("largest of three input number:"+max);
}
//-->
</script>
</head>
<body>
<form>
<input type="button" value="sq" onclick="sq();"/>
</form>
</body>
</html>
4.4
Similar to 4.2
4.5
To find legal or illegal name depending on the format.
Last name, first name, middle name
Where neither of the name can be more than 15 characters.
<html>
<head>
Divya, Asst. Prof. ISE dept. VCET puttur

Page 3

<script language="JavaScript">
<!--//
function sq()
{
var name=prompt( "Enter first number " , "" ).toLowerCase();
var alphaExp = /^[a-zA-Z]+[,][a-zA-Z]+[,][a-zA-Z]+$/;
var firstName = name.split(',');
if (alphaExp.test(name)&&firstName[0].length<=15)
{
alert("legal name");
}
else
{ alert("illegal name");
}
}
//-->
</script>
</head>
<body>
<form>
<input type="button" value="sq" onclick="sq();"/>
</form>
</body>
</html>

4.6
Input a line of text using prompt and sort words of the text in alphabetical order.
<html>
<head>
<script language="JavaScript">
<!--//
function sq()
{
str = prompt("Please input your sentence", "");
var words = str.split(" ");
words = words.sort();
words_len = words.length;
for (count = 0; count < words_len; count++)
document.write(words[count] + "<br/>");
}
//-->
</script>
</head>
Divya, Asst. Prof. ISE dept. VCET puttur

Page 4

<body>
<form>
<input type="button" value="sq" onclick="sq();"/>
</form>
</body>
</html>
4.7
Input a line of text and sorting order i.e ascending or descending using prompt and
sort words of the text based on sorting order given by the user.
<html>
<head>
<script language="JavaScript">
<!--//
function sq()
{
var order, str, words, word_len, count;
// Get the input
str = prompt("Please input your sentence", "");
order = prompt("What order? (ascending or descending)", "");
// If the order is recognized, issue an error message
if (order != "descending" && order != "ascending")
document.write("Error - order is incorrectly specified <br/>");
// Otherwise, do the sort, depending on the requested order
else {
var words = str.split(" ");
if (order == "ascending")
{
words = words.sort();
}
else
{
a= words.sort();
words=a.reverse();
}
// Write out the results
words_len = words.length;
for (count = 0; count < words_len; count++)
document.write(words[count] + "<br/>");
}
Divya, Asst. Prof. ISE dept. VCET puttur

Page 5

//-->
</script>
</head>
<body>
<form>
<input type="button" value="sq" onclick="sq();"/>
</form>
</body>
</html>
4.8
Modify the given array to remove all zero values.
<html>
<head>
<script language="JavaScript">
<!--//
var arr = new Array (0,2,5,9,7,0,5,0);
e_names(arr);
function e_names(arr)
{
len=arr.length-1;
for(i=len;i>=0;i--){
if(arr[i]==0)
{
arr.splice(i, 1);
}

}
for(i=arr.length-1;i>=0;i--){
document.write(arr[i],"<br/>");
}
}
//-->
</script>
</head>
<body>
</body>
</html>

Divya, Asst. Prof. ISE dept. VCET puttur

Page 6

4.9
Input the array of names and find the names that end in either ie or y
<html>
<head>
<script type = "text/javascript">
<!-// Function e_names
// Parameter: an array of strings
// Returns: the number of given strings that end
//
in either "ie" or "y"
function e_names(names) {
var len, index, count = 0;
len = names.length;
// Loop to use pattern matching to produce the count
for (index = 0; index < len; index++) {
position1 = names[index].search(/ie$/);
position2 = names[index].search(/y$/);
if (position1 + position2 > -2)
count++;
}
return count;
}
// -->
</script>
</head>
<body>
<script type = "text/javascript">
<!-// Function e_names tester
var new_names = new Array ("freddie", "boby", "mieke", "yahoo2", "georgey");
result = e_names(new_names);
document.write("The number of special names is: " + result + "<br/>");
// -->
</script>
</body>
</html>

4.10
Input the string and find the position of the leftmost vowel in the given string
<html>
<head>
<script type = "text/javascript">
Divya, Asst. Prof. ISE dept. VCET puttur

Page 7

<!-function first_vowel(str) {
for(var i = 0; i<str.length; i++)
{
if (str.charAt(i) =='a' || str.charAt(i) == 'e' || str.charAt(i) =='i'
|| str.charAt(i) == 'o' || str.charAt(i) == 'u' || str.charAt(i) == 'A' ||
str.charAt(i) == 'E' || str.charAt(i) =='I' || str.charAt(i) =='O' || str.charAt(i) == 'U')
{
document.write("The entered string is:" +str+ "<br/>");
document.write("The leftmost vowel is :"+str.charAt(i)+"<br/>");
var pos = i+1;
document.write("The position of the leftmost vowel " +str.charAt(i)+ " is:"
+pos+"\n");
exit;
}
}
document.write("The entered string is:" +str+ "<br/>");
document.write("The entered string has no vowels");
}
// -->
</script>
<script type = "text/javascript">
<!-// Function e_names tester
var str = "divya puTTur";
first_vowel(str);
// -->
</script>
</head>
<body>
</body>
</html>

4.11
Input the array of numbers and find the count of negatives, zeros and positive numbers
in the given array using switch statement.
<html>
<head>
<script type = "text/javascript">
<!-function counter(str) {
var neg=0;
var zero=0;
var pos=0;
for(var i = 0; i<=arr.length; i++)
{
switch(true)
{
Divya, Asst. Prof. ISE dept. VCET puttur

Page 8

case (arr[i] < 0):


neg++;
break;
case (arr[i] == 0): zero++;
break;
case (arr[i] > 0): pos++;
break;
}
}
document.write("number of zeros in the array", zero,"<br />");
document.write("number of pos in the array", pos,"<br />");
document.write("number of neg in the array", neg,"<br />");
}
// -->
</script>
<script type = "text/javascript">
<!-var arr= [-3,-5,0,78,343,-344,0,0];
counter(arr);
// -->
</script>
</head>
<body>
</body>
</html>

4.12
Match the string of given format:
String1, string2 letter
Both the strings must be lowercase except the first letter, letter must be uppercase.
If match return true or false.
<html>
<head>
<script type = "text/javascript">
<!-function tst_name(str)
{
var pattern=/^[A-Z][a-z]+,[ ][A-Z][a-z]+[ ][A-Z]$/;
if(str.match(pattern)) {
alert('true');
}
else{
alert('false');
}
}
// -->
Divya, Asst. Prof. ISE dept. VCET puttur

Page 9

</script>
<script type = "text/javascript">
<!-var str=prompt("enter the string", "Hi, We A");
tst_name(str);
// -->
</script>
</head>
<body>
</body>
</html>
4.13
To find average of each rows of the given array
<html>
<head>
<script type = "text/javascript">
<!-var arr = [[1, 2, 3, 4, 5], [5, 4, 3, 22, 1], [13, 2, 1, 4, 5]];
row_averages(arr);
function row_averages(arr) {
for ( i = 0; i < arr.length; i++)
{
var sum = 0;
for ( j = 0; j < arr[i].length; ++j)
{
sum = sum + arr[i][j];
}
avg = sum / arr[i].length;
alert(avg);
}

}
// -->
</script>
</head>
<body>
</body>
</html>

4.14
Reverse of a given number
<html>
<head>
Divya, Asst. Prof. ISE dept. VCET puttur

Page 10

<script type = "text/javascript">


<!-var n;
n = parseInt(prompt("enter the no:"));
var rn=0;
while(n>0)
{
r=parseInt(n%10);
n= parseInt(n/10);
rn= rn*10+r;
}
document.write("reverse:",+rn);
// -->
</script>
</head>
<body>
</body>
</html>

Divya, Asst. Prof. ISE dept. VCET puttur

Page 11