Question

I'm new to this page. I have some problem regarding this sorting that I made. This is the code:

<html> 
<body>
<p>Enter First Number:<input type="text"  id="1"></p>
<p>Enter Second Number:<input type="text"  id="2"></p>
<p>Enter Third Number:<input type="text"  id="3"></p>
<p>Enter Fourth Number:<input type="text"  id="4"></p>
<p>Enter Fifth Number:<input type="text"  id="5"></p>
<button type="button" onclick="SortThis()" >Sort</button>  

<script>
function SortThis()
{
var tempVar; 
var x; 
var y;  
var numbers = new Array[]; 
numbers[0]=document.getElementById("1"); 
numbers[1]=document.getElementById("2"); 
numbers[2]=document.getElementById("3"); 
numbers[3]=document.getElementById("4"); 
numbers[4]=document.getElementById("5");  

for(x=0; x<=4 ; x++) 
{ 
for(y=0; y<=4; y++) 
{ 
if(numbers[x]<numbers[y]) 
{ 
tempVar=numbers[x]; 
numbers[x]=numbers[y]; 
numbers[y]=tempVar; 
} } } }  

for(var x=0; x<=numbers.length ; x++) 
{ 
var output =document.getElementById("output");
output.innerHTML=(numbers[x] +  " , ");
} 
</script> 
<p id="output"></p>
</body> 
</html>

The problem here is that I can't get an output. I want an output that will print using innerhtml.

Was it helpful?

Solution

Array will be declared as Array() not [] the append statment will be this output.innerHTML += numbers[x]+','; and to take value you should give numbers[0]=document.getElementById("1").value; not numbers[0]=document.getElementById("1");

I am not very good with the formatting !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top