Can someone tell me why this JavaScript code isn't lining up an array in order?
- by DarkLightA
Live code: http://jsfiddle.net/fCUZC/
//INPUT ARRAY:
var input = [28,32,21,11,8,2,14,32,64];
//VARIABLE DECLARATION. a = highest number so far, b = position of that number
entireLoop:
for (var i = 1; i<=input.length; i++)
{
if(input[i] > input[i-1])
{
for(var o = i; o>=0; o--)
{
if(input[i-1] > input[o])
{
input.splice(i,0,input[o]);
input.splice((o+1),1);
continue entireLoop;
}
else if(input[o] > input[0])
{
input.splice(0,0,input[o]);
input.splice((o+1),1);
continue entireLoop;
}
}
}
}
document.write(input);
I'm trying to order the array from largest to smallest, but there's a 32 stuck somewhere. I know there's the sort method, but I'm a newbie and want to try this for myself.