我在为作业创建的书单排序时遇到问题。书籍的标题应该随着日期而移动,并在点击按钮后从最旧到最新进行排序。然而,我不知道为什么《堂吉诃德》和《白鲸》在点击按钮后没有出现。是我遗漏了什么还是我做错了什么?感谢您的帮助。
var names = new Array("To Kill a Mockingbird","Jaws","Don Quixote","Moby-Dick","Northern Lights");
var published = new Array("July 11, 1960", "February 1, 1974","July 11, 1615","October 18, 1851","November 12, 1995");
function display()
{
for(var i = 0; i < names.length; i++)
{
var temp = "cell" + i + "0";
document.getElementById(temp).innerHTML = names[i];
temp = "cell" + i + "1";
document.getElementById(temp).innerHTML = published[i];
}
}
function SortIt( )
{
var oldest = 0;
var index = 0;
var j = 0;
var k = 0;
var count = 0;
var temp = 0; var temp2 = "";
count = published.length;
count = names.length;
for (k =0; k <= (count -1); k++ )
{
oldest = Date.parse(published[k]);
index = k;
for (j = (k + 1); j <= (count-1); j++)
{
if (Date.parse(published[j]) < oldest )
{
oldest = Date.parse(published[j]);
index = j;
}
}
if(k != index)
{
temp = published[k];
published[k] = published[index];
published[index] = temp;
temp2 = names[k];
names[k] = index[k];
names[index] = temp2;
}
}
display();
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Program 9 - Selection Sort</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="Carbajal_Lab9.js"></script>
</head>
<body onload="display()">
<h1>Program 9 - Selection Sort</h1>
<table id ="workTable" border ="2">
<tr>
<td> <span id = "cell00"/> </td>
<td> <span id = "cell01"/> </td>
</tr>
<tr>
<td> <span id = "cell10"/> </td>
<td> <span id = "cell11"/> </td>
</tr>
<tr>
<td> <span id = "cell20"/> </td>
<td> <span id = "cell21"/> </td>
</tr>
<tr>
<td> <span id = "cell30"/> </td>
<td> <span id = "cell31"/> </td>
</tr>
<tr>
<td> <span id = "cell40"/> </td>
<td> <span id = "cell41"/> </td>
</tr>
</table>
<form>
<input type="button" value="Sort" onclick="SortIt()"/>
</form>
</body>
</html>
在中
temp = published[k];
published[k] = published[index];
published[index] = temp;
temp2 = names[k];
names[k] = index[k];
names[index] = temp2;
您将names[k]
设置为index[k]
,即使index
是int/计数器。
对我来说,有效的方法是创建published
和names
的克隆,这样,当您试图从旧列表中输入值时,您可以引用,例如oldnames
。
变量count
也被设置为一行中的2个不同的值,。我将这一部分替换为对Math.min
的调用,它从2个数组长度中选择最小的一个(防止无效索引查询(
最后,通过在javascript中创建inital表的循环,我为您节省了一些html编写
var names = new Array("To Kill a Mockingbird","Jaws","Don Quixote","Moby-Dick","Northern Lights");
var published = new Array("July 11, 1960", "February 1, 1974","July 11, 1615","October 18, 1851","November 12, 1995");
var count = Math.min(names.length,published.length);
function drawTable(){
for(var i = 0; i < count; i++)
{
document.getElementById('workTable').innerHTML+="<tr>"+
"<td> <span id="cell"+i+"0""+"/></td>"+
"<td> <span id="cell"+i+"1""+"/></td>"+
"</tr>"
}
}
function display()
{
for(var i = 0; i < count; i++)
{
var temp = "cell" + i + "0";
document.getElementById(temp).innerHTML = names[i];
temp = "cell" + i + "1";
document.getElementById(temp).innerHTML = published[i];
}
}
function SortIt( )
{
var oldPublished=published.slice();
var oldNames = names.slice();
var oldest = 0;
var index = 0;
var j = 0;
var k = 0;
var temp = 0; var temp2 = "";
for (k =0; k <= (count -1); k++ )
{
oldest = Date.parse(published[k]);
index = k;
for (j = (k + 1); j <= (count-1); j++)
{
if (Date.parse(published[j]) < oldest )
{
oldest = Date.parse(published[j]);
index = j;
}
}
if(k != index)
{
temp = published[k];
published[k] = oldPublished[index];
published[index] = oldPublished[k];
temp2 = names[k];
names[k] = oldNames[index];
names[index] = oldNames[k];
}
}
display();
}
drawTable();
<!DOCTYPE html>
<html lang="en">
<head>
<title>Program 9 - Selection Sort</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="Carbajal_Lab9.js"></script>
</head>
<body onload="display()">
<h1>Program 9 - Selection Sort</h1>
<table id ="workTable" border ="2">
</table>
<form>
<input type="button" value="Sort" onclick="SortIt()"/>
</form>
</body>
</html>