Home | Computer Science

write a program using .sort, .reverse, .pop, .push

<html>
<body>
<script type="text/javascript">
var students = new Array(prompt("enter the value"),prompt("enter the second value"),prompt("enter the third value"),prompt("enter the fourth value"),prompt("enter the fifth value"));
var i;
for (i=0;i<=4;i++){
document.write(students[i] + "<br />");
}
document.write("<br />The SORTED students array<br />");
students.sort();
for (i=0;i<=4;i++){
document.write(students[i] + "<br />");
}
document.write("<br />The REVERSED students array<br />");
students.reverse();
for (i=0;i<=4;i++){
document.write(students[i] + "<br />");
}

document.write("<br />THE students array after REMOVING the LAST item<br />");
students.pop();
for (i=0;i<=3;i++){
document.write(students[i] + "<br />");
}

 document.write("<br />THE students array after PUSH<br />");
 students.push("New Stuff");

for (i=0;i<=4;i++){
document.write(students[i] + "<br />");
}
</script>

</body>
</html>

output is not valid in this program you can check output by copying this coding to notepad and run the program.

write a program using prompt & table

<html>
<body>
<script>
var rows=prompt("enter the number of rows");
var cols=prompt("enter the number of cols");
var a=prompt("what you want to print");
if(rows == "" || rows == null)
 rows = 10;

 if(cols== "" || cols== null)
 cols = 10;
Table(cols,rows,a);
function Table(cols,rows,a){
var j=1;
 var output = "<table border='1' width='500' cellpadding='5' style='border:6px solid red;background-color:yellow;'>";
 for(i=1;i<=rows;i++)
 {
 output = output + "<tr>";
 while(j<=cols)
 {
 output = output + "<th style='color:blue;size:20;'>" + a + "</th>";
 j = j+1;
 }
 output = output + "</tr>";
 j = 1;
 }
 output = output + "</table>";
 document.write(output);
}
</script>
</body>
</html>