You are on page 1of 8

JavaScript

1. Write a program using JavaScript which accepts student information (name marks of 5 subs). Make all validations and display marksheet.

<html> <head> <title>Untitled Document</title> <script type="text/javascript">

function validate()

{

var nm=document.getElementById("name").value; var sb1=document.getElementById("s1").value; var sb2=document.getElementById("s2").value; var sb3=document.getElementById("s3").value;

var sb4=document.getElementById("s4").value; var sb5=document.getElementById("s5").value; var f=0;

if(nm==" " || nm==null)

{

alert("Enter Valid Name");

f=1;

}

if(sb1==" " || sb1==null || sb1<0 || sb1>100)

{

alert("Enter Valid Marks of Subject 1");

f=1;

}

if(sb2==" " || sb2==null || sb2<0 || sb2>100)

{

alert("Enter Valid Marks of Subject 2");

f=1;

}

if(sb3==" " || sb3==null || sb3<0 || sb3>100)

{

alert("Enter Valid Marks of Subject 3");

f=1;

}

f(sb4==" " || sb4==null || sb4<0 || sb4>100)

{

alert("Enter Valid Marks of Subject 4");

f=1;

}

f(sb5==" " || sb5==null || sb5<0 || sb5>100)

{

alert("Enter Valid Marks of Subject 3");

f=1;

}

if(f==1)

{

return false;

}

else

{

return true;

}

}

function dis()

{

var nm=document.getElementById("name").value; var sb1=document.getElementById("s1").value; var sb2=document.getElementById("s2").value; var sb3=document.getElementById("s3").value; var sb4=document.getElementById("s4").value; var sb5=document.getElementById("s5").value;

var ss1=new Number(sb1); var ss2=new Number(sb2); var ss3=new Number(sb3); var ss4=new Number(sb4); var ss5=new Number(sb5); var tot=ss1+ss2+ss3+ss4+ss5;

document.write("Total Is "+tot+"<br/><br/>"); document.write("Percent Is "+(tot/5));

}

</script>

</head>

<body> Student Name<input type="text" id="name"/><br/><br/> Subject 1:<input type="text" id="s1"/><br/><br/> Subject 2<input type="text" id="s2"/><br/><br/> Subject 3<input type="text" id="s3"/><br/><br/> Subject 4<input type="text" id="s4"/><br/><br/> Subject 5<input type="text" id="s5"/><br/><br/> <input type="button" id="b1" value="Submit" onclick="return validate();"/><br/><br/>

<input type="button" id="b2" value="Marksheet" onclick="return dis();"/><br/><br/>

</body>

</html>

2. Write a program is JavaScript which shows any 4 string manipulation operations.

<html>

<head>

<title>Untitled Document</title> <script type="text/javascript"> function ucase()

{

var str=document.getElementById("str").value; //str.toUpperCase(); document.getElementById("resultstr").value=str.toUpperCase();

}

function ln()

{

var str1=document.getElementById("str").value;

document.getElementById("resultstr").value=str1.length;
}

function sstr()

{

var str2=document.getElementById("str").value;

document.getElementById("resultstr").value=str2.substring(0,3);
}

function chat()

{

var str4=document.getElementById("str").value;

document.getElementById("resultstr").value=str4.toLowerCase();
}

</script>

</head>

<body> <input type="text" id="str"/><br/><br/> <input type="text" id="resultstr"/><br/><br/> <input type="button" id="b1" value="Upper" onclick="return ucase();"/> <input type="button" id="b1" value="Length" onclick="return ln();"/> <input type="button" id="b1" value="Sub String" onclick="return sstr();"/> <input type="button" id="b1" value="Lower" onclick="return chat();"/><br/><br/>

</body>

</html>

3. Write a JavaScript which checks the contents entered in a forms text element. If the text entered is in lower case, convert to upper case <html> <head> <title>Untitled Document</title>

</head>

<body> <input type="text" id="name" /><br/><br/> <input type="text" id="addr" /><br/><br/> <input type="text" id="city" /><br/><br/> <script type="text/javascript"> var nm=document.getElementById("name").value; var adr=document.getElementById("addr").value; var cty=document.getElementById("city").value;

document.getElementById("name").value=nm.toUpperCase();

document.getElementById("addr").value=adr.toUpperCase();

document.getElementById("city").value=cty.toUpperCase();

</script>

</body>

</html>

4. Write a JavaScript to accept a number from user. Make all validations and print that number in reverse order.

<html> <head> <title>Untitled Document</title> <script type="text/javascript"> function revr()

{

var rev=0; var rem=0; var n=document.getElementById("no").value; var n1=new Number(n); var str = "";

while(n1>0)

{

rem=n1%10;

rev=rev*10+rem;

n1=Math.floor(n1/10);

}

document.getElementById("no1").value=rev;

}

</script>

</head>

<body> <input type="text" id="no"/><br/> <input type="text" id="no1"/><br/> <input type="button" id="b1" value="Reverse" onclick="return revr();"/> </body> </html>

5. Write a JavaScript to calculate max, min sum and average of numbers in an array. Make all validations

<html> <head> <title>Untitled Document</title> </head>

<body> <script type="text/javascript">

var arr=Array(11,22,44,12,10,9); var mini=arr[0]; var maxm=arr[0]; var s=0 for( i=1; i<=arr.length; i++)

{

if(arr[i]<mini)

{

mini=arr[i];

}

if(arr[i]>maxm)

{

maxm=arr[i];

}

}

document.write("Array Is <br/>"); for(j=0; j<arr.length; j++)

{

document.write(arr[j]+"<br/>"); var k=new Number(arr[j]); s=s+k; a=s/arr.length

}

document.write("Minimum No is "+mini+"<br/><br/>"); document.write("Maximum No is "+maxm+"<br/><br/>"); document.write("Sum Of No is "+s+"<br/><br/> "); document.write("Average of No is "+a+"<br/>");

</script>

</body>

</html>

6. Write a program using JavaScript to print Fibonacci Series

<html> <head> <title> Fibonacci </title> </head> <body> <script type="text/javascript"> var f1=0; var f2=1;

var f3=0; var n=prompt("Enter No"," ");

document.write(f1+"<br/><br/>");

document.write(f2+"<br/><br/>");

for(i=3;i<=n;i++)

{

f3=f1+f2;

f1=f2;

f2=f3;

document.write(f3+"<br/><br/>");

}

</script>

</body>

</html>