Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

MUSKAN DHINGRA

19BCE1148
INTERNET AND WEB PROGRAMMING
JAVA SCRIPT
LAB HOMEWORK
QUESTION 1

CODE:

<html>

<head>

<style>

table,th,td{

border:2px solid black;

</style>

<script>

document.write( "<table> <tr> <th>Number</th> <th>Square</th> <th>Cube</th> </tr>" )

for(var n=0; n<=10; n++)

document.write( "<tr><td>" + n + "</td><td>" + n*n + "</td><td>" + n*n*n + "</td></tr>" )

document.write( "</table>" )

</script>

</head>

</html>
OUTPUT:

QUESTION 2:

PART A)

CODE:

<html>

<head>

<title>QUESTION 2</title>

</head>

<script>
function vowel()

var n = prompt("Enter a string: ","");

flag=0;

for(i=0;i<n.length && flag!=1 ;i++)

switch(n[i])

case 'a':

case 'e':

case 'i':

case 'o':

case 'u': alert("The position in the string of the left-most vowel is: " +(i+1));

flag = 1;

break;

default : break;

if(!flag)

alert("No Vowels found.");

</script>

<body onload="vowel()">

</body>
OUTPUT:

PART B:

CODE:

<html>

<head>

<title>QUES2-B</title>

</head>

<script type="text/javascript">

function rev()

var n = prompt("Enter a number: ","");


var str=0;

for(i=n.length-1;i>=0;i--)

str = str*10 + Number(n[i]);

alert(str);

</script>

<body onload="rev()">

</body>

OUTPUT:
QUESTION 3:

CODE:

<html>
<head>
<script type="text/javascript">
document.write( "<table border = 1 cellpadding = 5px> " )
for (var x=0; x<=15; x++) {
if (x === 0) {
document.write("<tr><td>" + x + " is even" + "</td></tr>");
}
else if (x % 2 === 0) {
document.write("<tr><td>" + x + " is even" + "</td></tr>");
}
else {
document.write("<tr><td>" + x + " is odd" + "</td></tr>");
}
}
</script>
</head>
<body>
</body>
</html>
OUTPUT:
QUESTION4:

CODE:

<html>

<head>

<title></title>

<script type="text/javascript">

function calc()

form=document.getElementById("form1");

sub1=form.sub1.value;

sub2=form.sub2.value;

sub3=form.sub3.value;

sub4=form.sub4.value;

total=parseInt(sub1)+parseInt(sub2)+parseInt(sub3)+parseInt(sub4);

form.total.value= total;

form.avg.value= parseInt(total)/4;

if(sub1<35||sub2<35||sub3<35||sub4<35)

{ form.result.value="Fail";}

else {form.result.value="Pass";}

// Grading system.

if(form.avg.value>90)

{form.grade.value="A";}

else if (form.avg.value>80 && form.avg.value<90)

{form.grade.value="B";}

else if (form.avg.value>70 && form.avg.value<80)


{form.grade.value="C";}

else if (form.avg.value>60 && form.avg.value<70)

{form.grade.value="D";}

else if (form.avg.value>=35 && form.avg.value<60)

{form.grade.value="E";}

</script>

</head>

<body>

<form id="form1">

<br /><br />

Enter the Name : <input type="text" size="20" name="name"/><br />

Marks in sub1 : <input type="text" size="20" name="sub1"/><br />

Marks in sub2 : <input type="text" size="20" name="sub2"/><br />

Marks in sub3 : <input type="text" size="20" name="sub3"/><br />

Marks in sub4 : <input type="text" size="20" name="sub4"/><br />

<input type="button" name="btn" value="Calculate" onclick="calc()"/><br />

Total : <input type="text" size="20" name="total"/><br />

Average : <input type="text" size="20" name="avg"/><br />

Result : <input type="text" size="20" name="result"/><br />

Grade : <input type="text" size="20" name="grade"/><br />

<input type="reset" name="Btn2" value="Reset"/>


</form>

</body>

</html>

OUTPUT:

QUESTION 5:

<html>

<head>

<meta charset="utf-8">

<title>Sum the multiples of 3 and 5 under 1000</title>

<script>

var sum = 0;

for (var x = 0; x < 1000; x++)

if (x % 3 === 0 || x % 5 === 0)
{

sum += x;

alert(sum);

</script>

</head>

<body>

</body>

</html>

OUTPUT:

QUESTION 6:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>QUESTION-6</title>

</head>

<body>

<script>

var arr = [2,50,20,12,89,45,23,90,11,0];


var n =arr.length;

var pos_i, pos_j, ele_i, ele_j;

var dist=999;

var tmp;

for(var i=0;i<n;i++) {

for(var j=i+1;j<n;j++) {

if(arr[i]>arr[j])

tmp=arr[i]-arr[j];

else

tmp=arr[j]-arr[i];

if(tmp<dist) {

dist=tmp;

pos_i=i; pos_j=j;

ele_i=arr[i]; ele_j=arr[j];

pos_i++;

pos_j++;

document.write("Array : [" + arr + "] <br>");

document.write("Shortest Distance : " + dist + "<br>");

document.write("Elements : " + ele_i + " and " + ele_j + "<br>");

document.write("Position : " + pos_i + " and " + pos_j);

</script>

</body>

</html>
OUTPUT:

QUESTION 7:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>QUESTION-7</title>

</head>

<body>

<script>

var str=prompt("Enter the word");

document.write("The last letter "+str.slice(-1)+"<br>");

if(str.slice(-1)=='y')

document.write("The plural "+str.slice(0,-1)+"ies");

else

if(str.slice(-1)=='s'||str.slice(-1)=='h')

document.write("The plural "+str.slice(0,-1)+"es");

else
document.write("The plural "+str.slice(0,-1)+"s");

</script>

</body>

</html>

OUTPUT:

You might also like