You are on page 1of 19

Age.

js:
var age=prompt("Enter your year of birth");

age=parseInt(age);

var month=prompt("Enter your month of birth");

month=parseInt(month);

var day=prompt("Enter your day of birth");

day=parseInt(day);

var today = new Date();

var presentyear=today.getFullYear();

var presentmonth=today.getMonth();

var presentday=today.getDay();

ageyr=presentyear-age;

monthyr=presentmonth-month;

dayyr=presentday-day;

document.write("Your age is:");

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

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

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

array_properties:
<html>
<head>
<title>Single dimensional array properties and
methods</title>
</head>
<body>
<script>

colors=prompt("Select the property name that is to be


demonstrated" +1+ "Push" + "<br />" +2+ "Pop" + "<br
/>" +3+ "Shift" + "<br />");
switch(colors)
{
case "1" : var colors=new
Array("red","yellow","orange");
document.write("element will be
pushed into an array");
var color=prompt("Enter a color
to be pushed into an array");
colors.push(color);

document.write("Pushed="+color);
break;
case "2" : document.write("Element is popped out of an
array");
var prop=colors.pop();
document.write("Popped="+prop);
break;
case "3" : document.write("Element is shifted in an
array");
var prop=colors.shift();

document.write("Shifted="+prop);
break;
default: {
document.write("Error -invalid choice: ");
}
}
</script>
</body>
</html>

array_properties_methods:
<html>
<head>
</head>
<body>
<script>
var colors=new Array("red","yellow","blue");
document.write("Length of an array <br>");
var Length=colors.length;
document.write("Length of an
array="+Length+"<br>");
document.write("Pushing an element in to an array
<br>");
var color=prompt("Enter the color you want to
push <br>");
var prop1=colors.push(color);
document.write("Pushed="+prop1+"<br>");
document.write("\nPopping an element out of an
array <br>");
var prop2=colors.pop();
document.write("Popped="+prop2+"<br>");
document.write("\nShifting an element in an array
<br>");
var prop3=colors.shift();
document.write("Element shifted="+prop3+
"<br>");
document.write("\nUnshifting an element in an
array <br>");
var prop4=colors.unshift();
document.write("Element unshifted="+prop4+
"<br>");
</script>
</body>
</html>

Date:
<!DOCTYPE html>
<!-- date.html
a document for date.js
-->
<html lang = "en">
<head>
<title> date.html</title>
<meta charset = "utf-8" />
</head>
<body>
<script type="text/javascript" src = "age.js" >
</script>
</body>
</html>

Date.js:
//date.js
//Illustrate the use of the date
alert("Start date");
var today = new Date();

var dateString=today.toLocalString();
var day=today.getDay();

var month=today.getMonth();

var year=today.getFullYear();

var timeMilliseconds=today.getTime();

var hour=today.getHours();

var minute=today.getMinutes();

var second=today.getSeconds();

var milliseconds=today.getMilliseconds();
//display

document.write(

"Date: " + dateString + "<br


/>",

"Day: " + day + "<br />",

"Month: " + month + "<br />",

"Year: " + year+ "<br />",

"TimeInMilliseconds: " + timeMilliseconds + "<br


/>",

"Hour: " + hour + "<br />",


"Minute: " + minute + "<br />",

"Second: " + second + "<br />",

"milliseconds: " + milliseconds + "<br />");

Date2.html:
<!DOCTYPE html>
<!-- date.html
a document for date.js
-->
<html lang = "en">
<head>
<title> date.html</title>
<meta charset = "utf-8" />
</head>
<body>
<script type="text/javascript" src = "date.js" >
</script>
</body>
</html>

Factorial:
<html>
<head>
<script>
function factorial()
{
var fact=1;
var num=prompt("Enter a
number"," ");
num=parseInt(num);
for(var i=1;i<=num;i++)
fact=fact*i;
return fact;
}
document.write("The factorial= "+factorial());
</script>
</head>
<body>

</body>
</html>

Fib:
<html>
<head>
<script>
function fib()
{
fib1=0;
fib2=1;
var num=prompt("Enter
number:"," ");
num=parseInt(num);
document.write("<p>" +0+ "</p>");
for(var i=1;i<=num;i++)
{
fib3=fib1+fib2;
document.write("<p>" +fib3+ "</p>");
fib1=fib2;
fib2=fib3;

}
return fib3;
}
document.write(fib());
</script>
</head>
</html>

Nested array.html:
<!DOCTYPE html>
<!-- date.html
a document for date.js
-->
<html lang = "en">
<head>
<title> Array of arrays</title>
<meta charset = "utf-8" />
</head>
<body>
<script type="text/javascript" src = "date.js" >
</script>
</body>
</html>

Nested array.js
// An example to illustrate an array of arrays
// create an array object with these arrays as its
elements

for(var row=0;row<=1;row++)
for(var col = 0; col<=1;col++)
{
var
row_elements=prompt("Enter the elements of
matrix");

row_elements[row]
[col]=parseInt(row_elements[row][col]);
}
// display the elements of nested list
for (var row=0; row<=1; row+
+)
for(var col = 0; col<=1;col++)

document.write(row_elements[row][col], " ");

nested_arrays.html:
// An example to illustrate an array of arrays
// create an array object with these arrays as its
elements

for(var row=0;row<=1;row++)
for(var col = 0; col<=1;col++)
{
var
row_elements=prompt("Enter the elements of
matrix");
row_elements[row]
[col]=parseInt(row_elements[row][col]);
}
// display the elements of nested list
for (var row=0; row<=1; row+
+)
for(var col = 0; col<=1;col++)

document.write(row_elements[row][col], " ");

nested_arrays.js:
// An example to illustrate an array of arrays
// create an array object with these arrays as its
elements
var nested_array = [[2,4,6],
[1,3,5],
[10,20,30]
];
// display the elements of nested list
for (var row=0; row<=2; row+
+)
{
document.write("Row ", row ,
":");
for(var col = 0; col<=2;col++)

document.write(nested_array[row][col], " ");


document.write("<br />");
}

Sum:
<html>
<head>
<title>Add two integers and display the
sum</title>
</head>
<body>
<script>
var a=prompt("Enter an integer"," ");
var b=prompt("Enter an integer"," ");
var sum=parseInt(a)+parseInt(b);
/*document.write("Sum of two integers="
+sum);*/
/*alert("Sum of two integers=" +sum);*/
var bord=prompt("Set border of table as 0, 2, 4, 6,
8","1");

var bord=parseInt(bord);
if(bord===0 || bord===2 || bord===4 || bord===6 ||
bord===8)
{

document.write("<table
border="+bord+"><caption>Arithmethic
operations</caption>" +
"<tr>" +
"<td> number1 </td>
<td> number2 </td> <td> Sum </td> </tr>" +
"<tr>"+
"<td>"
+a+"</td><td>"+b+ "</td><td>" +sum
+ "</td></tr></table>");
}
else
{
alert("Enter a valid border size");
}

</script>

</body>
</html>

You might also like