Is it possible to sort arrays in ascending/descending order using the for loop JavaScript?
I've been learning JS going through a few practice questions in a textbook before a class test.
Any pointers would be appreciated!
a = Number(prompt("a:"));
b = Number(prompt("b:"));
c = Number(prompt("c:"));
d = Number(prompt("d:"));
e = Number(prompt("e:"));
// find largest element
var test = [a, b, c, d, e];
var biggest = -Infinity;
var biggest_index = -1; {
for (i = 0; i < test.length; i++) {
if (test[i] > biggest) {
biggest = test[i];
biggest_index = i;
}
else;
}
alert("The biggest element is " + biggest + " at index " + biggest_index);
}
// move largest element of array to the last index
test[test.length] = biggest;
// get rid of copy
test[biggest_index] = 0;
alert("Unsorted: " + test);
// shuffle ??
alert(Math.max.apply(null, array));is pretty short. But it doesn't use a for loop. :-)0will not remove it from the array, you need to use Array.prototype.splice() for that, I'll let you look it up in ECMA-262.