dear all...please help me...i want a program in java script...which has only a text box in which we enter a numbers or string seperated by comma ....should be sorted and values to be displayed...ple ase any body write program for me...i am not getting it...pzz..anybo dy help...
javascript sorting program in html
Collapse
X
-
getting the textbox value is simple:
note that <textbox> stands for a reference to the textbox element. (so plain copy/past won’t work)Code:var value = <textbox>.value;
the functions you’ll likely use next are String.split() and Array.sort().Comment
-
Code:<html> <head> <title> Sorting Numbers....!!!!! </title> <script type="text/javascript"> var myArray = []; function bubblesort() { var inputValue = document.getElementById("valueField"); for (var i = 0; i < inputValue.value; ++i) { inputValue.innerHTML = ""; myArray[i] = document.getElementById("valueField").value; } var length=myArray.length; var returnArray=new Array(length); for (var i =0; i < (arrayToSort.length-1); i = i+1) { if ((arrayToSort[i+1]) < (arrayToSort[i])) { var temp = arrayToSort[i+1]; arrayToSort[i+1] = arrayToSort[i]; arrayToSort[i] = temp; } } return returnArray; </script> </head> <body> <form> Enter number of values: <input id="valueField" type="number"> <input id="submitButton" value="Submit" type="button"> </form> </body> </html>
this is what so far i did...plzzz help....
any corrections and further completionComment
-
#1 input fields do not have an innerHTML property. they’re empty elements.
#2 why creating an explicit bubblesort implementation? doesn’t the native sort work for you?
#3 on line 11 you treat the input as a max value. on line 14 you put the same value that many times in an array, i.e. you get an array of size n with the number n as each element. due to that the sorting afterwards does not make any sense.
#4 if your input is not a single number (e.g. a comma-separated list of numbers) your code will bail out due to impossible number conversion (i.e. resulting in NaN (not a number))Comment
Comment