2

我在尝试对为作业创建的书单进行排序时遇到问题。书籍的标题应该随着日期移动,并在单击按钮后从最旧到最新排序。但是,我不知道为什么点击按钮后唐吉诃德和白鲸没有出现。有什么我遗漏或做错了吗?帮助表示赞赏。

    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>

4

1 回答 1

1

 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/counter 。

对我有用的是创建 and 的克隆publishednames这样当您尝试从旧列表中输入一个值时,您可以引用例如oldnames.

该变量count也连续设置为 2 个不同的值。我已将此部分替换为对 的调用Math.min,它选择 2 个数组长度中最小的一个(防止无效索引查询)

最后,我通过在 javascript 中创建初始表的循环为您节省了一些 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>

于 2021-07-22T21:19:12.057 回答