In: Computer Science
JavaScript (HTML) Task:
Please read 10 numbers that are list below, sort the numbers and then print those numbers.
10 numbers = { 9, 3, 2, 1, 10, 30, 4, 6, 7, 8}
[Reference JavaScript code]
<html>
<body>
<H1>prompt()</h1>
<p id="pro"></p>
<script>
// Array creation
var num= new Array();
var ix = 0;
// Read 10 numbers
for (ix = 0; ix < 10; ix++)
{num[ix] = prompt("Enter a number");
}
// Write 10 numbers
document.writeln( num + "<br>");
// Write 10 numbers one by one
for(ix = 0; ix < 10; ix++)
{
document.writeln( num[ix] + "<br>");
}
</script>
</body>
</html>
Code:
<html>
<body>
<script>
var numbers=new Array();
//array declaration
for (var i=0;i<10;i++)
//iterating loop ten times to take 10
numbers
numbers[i]=prompt("Enter Number " +
(i+1) + "") //asking user to enter
the 10 numbers
document.writeln("Given Array is : " +
numbers) //printing the given
array
document.writeln("<br>"+"The Sorted Array is : "
+ numbers.sort(function(n1, n2){return n1 - n2}))
//sorting the array using array.sort() function by
default sort takes the values as strings so we will put compare
function in it.
var sorted_numbers=numbers.sort(function(n1,
n2){return n1 - n2}); //sorting the array again to show values line
by line
//sorting and storing it in sorted_numbers
document.writeln("<br>The Sorted Array :
<br>")
//printing sorted array numbers one by one
for (var i=0;i<10;i++)
document.writeln(sorted_numbers[i] + "<br>")
</script>
</body>
</html>
Code and Output Screenshots:
Note: if you have any queries please post a comment thanks a lot.. always available to help you..