This website crashes when executed:
<head>
<script>
var numbersList = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 19, 17, 15, 13, 11, 9, 7, 5, 3, 1];
var orderedList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
var count = 0;
while (numbersList !== orderedList) {
if (numbersList[count] > numbersList[count + 1]) {
var lowNum = numbersList[count + 1];
var highNum = numbersList[count];
numbersList[count] = lowNum;
numbersList[count + 1] = highNum;
}
if (count === 19) {
count = 0;
} else {
count = count + 1;
}
}
</script>
</head>
</html>
How can I fix this issue?
Based on my understanding, the logic is correct as per my intention. This is simply a recreational experiment.
After additional development, the comparison problem has been resolved:
<!doctype html>
<html>
<head>
<script>
var passes = 0;
var swaps = 0;
var numList = [25, 21, 4, 23, 32, 2, 40, 8, 27, 9, 29, 33, 31, 14, 12, 16, 35, 18, 37, 20, 39, 19, 38, 17, 36, 15, 34, 13, 6, 11, 30, 10, 28, 7, 26, 5, 1, 3, 22, 24];
var index = 0;
var swappedNum = 0;
var inOrder = 0;
while (inOrder !== 1) {
if (numList[index] > numList[index + 1]) {
var lowerVal = numList[index + 1];
var higherVal = numList[index];
numList[index] = lowerVal;
numList[index + 1] = higherVal;
swappedNum = 1;
swaps = swaps + 1;
}
if (index === numList.length - 1) {
if (swappedNum === 0) {
inOrder = 1;
}
swappedNum = 0;
index = 0;
} else {
index = index + 1;
}
passes = passes + 1;
}
alert("Number List: " + numList);
alert("Passes: " + passes + ", Swaps: " + swaps);
</script>
(Similar to how the "cooky monster" suggests)