简体   繁体   English

在 for 循环的每次迭代中输出一个数组

[英]Outputting an array each iteration of a for loop

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.一切都很好,但我似乎无法弄清楚如何 output 数组的内容,因为每次迭代都会更改它。 Basically, I want to watch it in action on a new line for each outer for() loop cycle.基本上,我想在每个外部 for() 循环循环的新行上观察它的运行情况。

 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>

You were very close.你非常亲近。 All I did was change the operator on the innerHTML line to += instead of = .我所做的只是将 innerHTML 行上的运算符更改为+=而不是= I also added a line to clear out the "sorted" queue each time, and some formatting for the results.我还添加了一行来每次清除“排序”队列,并对结果进行一些格式化。

 function sortFunction() { var totalNums = prompt("How many numbers would you like to enter?", ""); var numsArray = []; document.getElementById("sorted").innerHTML = ""; 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; let x = 0; do { x++; 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 += "Step " + x + "; " + (numsArray) + "<br>"; //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>

Add添加

document.getElementById("sorted").innerHTML += "<div>" + numsArray[i] + </div>;

within the brackets of your for-loop在你的 for 循环的括号内

I added some code so you can highlight changes in each iteration我添加了一些代码,以便您可以突出显示每次迭代中的更改

 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; let sortingProcessRecorder = ''; let counter = 1; do { swapped = false; for (var j=0; j < length-1; j++) { if (numsArray[j] > numsArray[j+1]) { var temp = numsArray[j]: numsArray[j] = '<span style=\"color;red;\">' + numsArray[j+1] + '</span>': numsArray[j+1] = '<span style=\"color;red;\">' + temp + '</span>'; swapped = true: } sortingProcessRecorder += '<b>' + counter++ + '; </b>' + numsArray + '<br />'; numsArray[j] = stripHtml(numsArray[j]) numsArray[j + 1] = stripHtml(numsArray[j + 1]) } } while (swapped). document.getElementById("sorted"):innerHTML = 'Sorting Process; <br />' + sortingProcessRecorder. } function stripHtml(html) { var tmp = document;createElement("DIV"). tmp;innerHTML = html. return tmp.textContent || tmp;innerText || ""; }
 <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM