0% found this document useful (0 votes)
3 views3 pages

Program 14

The document describes a JavaScript program that sorts user-inputted numbers in ascending or descending order. It outlines an algorithm for reading input, converting it to an array, sorting the values, and displaying the results. The program includes HTML and JavaScript code for the user interface and functionality.

Uploaded by

jgame3145
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Program 14

The document describes a JavaScript program that sorts user-inputted numbers in ascending or descending order. It outlines an algorithm for reading input, converting it to an array, sorting the values, and displaying the results. The program includes HTML and JavaScript code for the user interface and functionality.

Uploaded by

jgame3145
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

14.

JavaScript Program for Sorting Values

ALGORITHM:
1. Start the program
2. Enter numbers in the input box (comma separated)
3. Read the entered values from the textbox
4. Convert the input into an array using split(",")
5. Convert each element into integer using parseInt()
6. Perform sorting
Use x - y for ascending order

Use y - x for descending order

7. Display the sorted result and Stop

PROGRAM 14:
<!DOCTYPE html>
<html>
<body>
<h3>Sorting Values</h3>
Enter numbers: <br>
<input id="inp"><br><br>
<button onclick="asc()">Ascending</button>
<button onclick="desc()">Descending</button>
<button onclick="clr()">Clear</button>
<p id="out"></p>

<script>
function asc(){
var a=[Link]("inp").[Link](",");
for(var i=0;i<[Link];i++) a[i]=parseInt(a[i]);
[Link](function(x,y){return x-y});
[Link]("out").innerHTML="Ascending: "+a;
}
function desc(){
var a=[Link]("inp").[Link](",");
for(var i=0;i<[Link];i++) a[i]=parseInt(a[i]);
[Link](function(x,y){return y-x});
[Link]("out").innerHTML="Descending: "+a;
}
function clr(){
[Link]("inp").value="";
[Link]("out").innerHTML="";
}
</script>
</body>
</html>
OUTPUT:

You might also like