I have a script which takes number inputs from user, assigns them to an array, then uses the bubble sort method to put these numbers in order.
Everything is working great, but I can't seem to figure out how to output the contents of the array AS it is being changed each iteration. Basically, I want to watch it in action on a new line for each outer for() loop cycle.
function sortFunction() { var totalNums = prompt("How many numbers would you like to enter?",""); var numsArray = []; for(i=0; i<totalNums; ++i) { var nums = prompt("Please enter number " ,""); if(nums != 'x') { numsArray[i] = parseInt(nums); document.getElementById("unsorted").innerHTML = "Orignal Numbers: " + numsArray; } } var length = numsArray.length; var swapped; do { swapped = false; for (var j=0; j < length-1; j++) { if (numsArray[j] > numsArray[j+1]) { var temp = numsArray[j]; numsArray[j] = numsArray[j+1]; numsArray[j+1] = temp; swapped = true; } } document.getElementById("sorted").innerHTML = (numsArray); //This is where I am needing help } while (swapped); } <p> Click the button to enter and display an array of numbers! </p> <button onclick="sortFunction()">Click Me</button> <div id ="unsorted">Unsorted</div> <div id ="sorted">Sorted</div>
innerHTML += something, or storing the intermediate stages in an array of arrays?