0% found this document useful (0 votes)
2 views

javascript programs

The document contains various JavaScript code snippets demonstrating basic programming concepts such as arithmetic operations, conditional statements, loops, functions, and form validation. It includes examples of calculating sums, areas, simple interest, and validating user input. Additionally, it showcases object creation, array manipulation, and string operations.

Uploaded by

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

javascript programs

The document contains various JavaScript code snippets demonstrating basic programming concepts such as arithmetic operations, conditional statements, loops, functions, and form validation. It includes examples of calculating sums, areas, simple interest, and validating user input. Additionally, it showcases object creation, array manipulation, and string operations.

Uploaded by

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

//sum of two numbers

<script>
var a=10;
var b=20;
var c=a+b;
document.write(c);
</script>

//area of rectangle
<script>
var l=10;
var b=20;
var a=l*b;
document.write(a);
</script>

//simple interest
<script>
var p=10000;
var t=2;
var r=5;
var i=(p*t*r)/100;
document.write(i);
</script>

//if condition
<script>
var a=20;
if(a>=18)
{
document.write("valid for vote");
}
</script>

//if…else condition for even or odd


<script>
var a=20;
if(a%2==0)
{
document.write("a is even number");
}
else
{
document.write("a is odd number");
}
</script>

//if…else condition for vote


<script>
var a=10;
if(a>=18)
{
document.write("valid for vote");
}
else
{
document.write("invalid for vote ");
}
</script>
//if…else if condition
<script>
var a=20;
if(a==10)
{
document.write("a is equal to 10");
}
else if(a==15)
{
document.write("a is equal to 15");
}
else if(a==20)
{
document.write("a is equal to 20");
}
else
{
document.write("a is not equal to 10, 15 or 20");
}
</script>

//if…else if condition for largest among three nos


<script>
var a=10;
var b=20;
var c=30;
if(a>b && a>c)
{
document.write("a is largest");
}
else if(b>a && b>c)
{
document.write("b is largest ");
}
else
{
document.write("c is largest ");
}
</script>

//if…else if condition for smallest among three nos


<script>
var a=10;
var b=20;
var c=30;
if(a<b && a<c)
{
document.write("a is smallest");
}
else if(b<a && b<c)
{
document.write("b is smallest");
}
else
{
document.write("c is largest ");
}
</script>
//switch case for day
<script>
var day=6;
switch(day)
{
case 1:
document.write (“sunday”);
break;
case 2:
document.write (“monday”);
break;
case 3:
document.write (“tuesday”);
break;
case 4:
document.write (“wednesday”);
break;
case 5:
document.write (“thursday”);
break;
case 6:
document.write (“friday”);
break;
case 7:
document.write (“saturday”);
break;
default:
document.write (“invalid day”);
}
</script>

//switch case for menu


<script>
var menu=6;
switch(menu)
{
case 1:
document.write (“momo”);
break;
case 2:
document.write (“pizza”);
break;
case 3:
document.write (“burgur”);
break;
case 4:
document.write (“yamari”);
break;
case 5:
document.write (“thukpa”);
break;
case 6:
document.write (“dossa”);
break;
case 7:
document.write (“fri rice”);
break;
default:
document.write (“sorry invalid item”);
}
</script>

//switch case for month


<script>
var month=6;
switch(month)
{
case 1:
document.write (“January”);
break;
case 2:
document.write (“february”);
break;
case 3:
document.write (“march”);
break;
case 4:
document.write (“april”);
break;
case 5:
document.write (“may”);
break;
case 6:
document.write (“june”);
break;
case 7:
document.write (“july”);
break;
case 8:
document.write (“august”);
break;
case 9:
document.write (“sepember”);
break;
case 10:
document.write (“october”);
break;
case 11:
document.write (“november”);
break;
case 12:
document.write (“december”);
break;
default:
document.write (“invalid month”);
}
</script>

//switch case for grade


<script>
var grade='b';
var result;
switch(grade)
{
case 'a':
result+=" a grade";
case 'b':
result+=" b grade";
case 'c':
result+=" c grade";
default:
result+=" no grade";
}
document.write(result);
</script>

//1 to 5 using for loop


<script>
var i;
for (i=1; i<=5; i++)
{
document.write(i + "<br/>")
}
</script>

//create series like 1 3 5 7 9 using for loop


<script>
var i;
for (i=1; i<=10; i=i+2)
{
document.write(i + "<br/>")
}
</script>

//create series like 2 4 6 8 10 using for loop


<script>
var i;
for (i=2; i<=10; i=i+2)
{
document.write(i + "<br/>")
}
</script>

//natural number up to 10 using for loop


<script>
var i;
for (i=1; i<=10; i++)
{
document.write(i + "<br/>")
}
</script>

//whole number up to 10 using for loop


<script>
var i;
for (i=0; i<=10; i++)
{
document.write(i + "<br/>")
}
</script>

// 5 times your name using for loop


<script>
var i;
for ( i=1; i<=5; i++)
{
document.write("bhagwati" + "<br/>")
}
</script>
//5 to 1 using for loop
<script>
var i;
for (i=5; i>=1; i--)
{
document.write(i + "<br/>")
}
</script>

//1 to 5 using while loop


<script>
var i=1;
while (i<=5)
{
document.write(i + "<br/>");
i++;
}
</script>

// 5 times your name using while loop


<script>
var i=1;
while (i<=5)
{
document.write("bhagwati" + "<br/>");
i++;
}
</script>

//5 to 1 using while loop


<script>
var i=5;
while (i>=1)
{
document.write(i + "<br/>");
i--;
}
</script>

//1 to 5 using do…while loop


<script>
var i=1;
do
{
document.write(i + "<br/>");
i++;
}
while (i<=5);
</script>

// 5 times your name using do…while loop


<script>
var i=1;
do
{
document.write("bhagwati" + "<br/>");
i++;
}
while (i<=5);
</script>
//5 to 1 using do…while loop
<script>
var i=5;
do
{
document.write(i + "<br/>");
i--;
}
while (i>=1);
</script>

//function1
<script>
function msg()
{
alert("hello! this is message");
}
</script>
<input type="button" onclick="msg()" value="call function"/>

//function2
<script>
function getinfo()
{
return "hello world! how r u?";
}
</script>
<script>
document.write(getinfo());
</script>

//function using sum of two numbers


<script>
var add=new function("num1","num2","return num1+num2");
document.writeln(add(2,5));
</script>

//power function
<script>
var pow=new function("num1","num2","return math.pow(num1,num2)");
document.writeln(pow(2,3));
</script>

//create object
<script>
var emp=new object();
emp.id=101;
emp.name="ravi malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>

//this keyword to create object


<script>
function emp(id,name,salary)
{
this.id=id;
this.name=name;
this.salary=salary;
this.changesalary=changesalary;
function changesalary(othersalary)
{
this.salary=othersalary;
}
}
e=new emp(103,"sonoo jaiswal",30000);
document.write(e.id+" "+e.name+" "+e.salary);
e.changesalary(45000);
document.write("<br>"+e.id+" "+e.name+" "+e.salary);
</script>

//array in javascript
<script>
var emp=["sonoo","vimal","ratan"];
for (i=0;i<emp.length;i++)
{
document.write(emp[i] + "<br/>");
}
</script>

// create object using array using new


<html>
<body>
<script>
var i;
var emp = new array();
emp[0] = "arun";
emp[1] = "varun";
emp[2] = "john";
for (i=0;i<emp.length;i++)
{
document.write(emp[i] + "<br>");
}
</script>
</body>
</html>

//strcat function
<script>
var s1="javascript ";
var s2="concat example";
var s3=s1.concat(s2);
document.write(s3);
</script>

//strlwr function
<!doctype html>
<html>
<body>
<script>
var s1="javascript ";
var s2="concat example";
var s3=s1+s2;
document.write(s3);
</script>
</body>
</html>
//strupr function
<!doctype html>
<html>
<body>
<script>
var s1="javascript touppercase example";
var s2=s1.touppercase();
document.write(s2);
</script>
</body>
</html>

//form create
<form name="form1">
enter name:<input type="text" name="name"/>
<input type="button" onclick="printvalue()" value="print name"/>
</form>

//radio button
<script type="text/javascript">
function totalelements()
{
var allgenders=document.getelementsbyname("gender");
alert("total genders:"+allgenders.length);
}
</script>
<form>
male:<input type="radio" name="gender" value="male">
female:<input type="radio" name="gender" value="female">
<input type="button" onclick="totalelements()" value="total genders">
</form>

//form validation
<script>
function validateform()
{
var name=document.myform.name.value;
var password=document.myform.password.value;
if (name==null || name=="")
{
alert("name can't be blank");
return false;
}else if(password.length<6)
{
alert("password must be at least 6 characters long.");
return false;
}
}
</script>
<body>
<form name="myform" method="post" action="abc.jsp" onsubmit="return validateform()" >
name: <input type="text" name="name"><br/>
password: <input type="password" name="password"><br/>
<input type="submit" value="register">
</form>

//password validation
<script type="text/javascript">
function matchpass()
{
var firstpassword=document.f1.password.value;
var secondpassword=document.f1.password2.value;
if(firstpassword==secondpassword)
{
return true;
}
else
{
alert("password must be same!");
return false;
}
}
</script>
<form name="f1" action="register.jsp" onsubmit="return matchpass()">
password:<input type="password" name="password" /><br/>
re-enter password:<input type="password" name="password2"/><br/>
<input type="submit">
</form>

//number validation
<script>
function validate()
{
var num=document.myform.num.value;
if (isnan(num))
{
document.getelementbyid("numloc").innerhtml="enter numeric value only";
return false;
}
else
{
return true;
}
}
</script>
<form name="myform" onsubmit="return validate()" >
number: <input type="text" name="num"><span id="numloc"></span><br/>
<input type="submit" value="submit">
</form>

// creating object
<script>
var emp=new object();
emp.id=101;
emp.name="ravi malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>

//creating constructor
<script>
function emp(id,name,salary)
{
this.id=id;
this.name=name;
this.salary=salary;
}
e=new emp(103,"vimal jaiswal",30000);
document.write(e.id+" "+e.name+" "+e.salary);
</script>

You might also like