-1

我正在尝试创建一个函数,它将一个音乐专辑与另一个专辑进行比较,但我的代码没有运行。我想知道为什么,因为我真的看不出问题出在哪里以及为什么会出现问题。非常感谢。

/********* GLOBAL VARIABLES *********/
var BR = "<br />";
var ES = " ";
/********* FUNCTIONS *********/

function compare(album[0], album[1]) {
    var sameAlbums = false;

    //There has to be an easier way to do all this...
    if (album[0].artistName === album[1].artistName && album[0].albumTitle === album[1].albumTitle && album[0].releaseYear === album[1].releaseYear && album[0].ifHQ === album[1].ifHQ && album[0].tracks[0] === album[1].tracks[0] && album[0].tracks[1] === album[1].tracks[1] && album[0].tracks[2] === album[1].tracks[2] && album[0].tracks[3] === album[1].tracks[3] && album[0].tracks[4] === album[1].tracks[4] && album[0].tracks[5] === album[1].tracks[5] && album[0].tracks[6] === album[1].tracks[6] && album[0].tracks[7] === album[1].tracks[7] && album[0].tracks[8] === album[1].tracks[8] && album[0].tracks[9] === album[1].tracks[9] && album[0].tracks[10] === album[1].tracks[10])

    {
        sameAlbums = true;
    }
    return sameAlbums;
}

/********* MAIN *********/

function main() {

    var alb = new album(2);


    for (var i = 0; i < album.length; i++) {
        album[i] = {
            artistName: "",
            albumTitle: "",
            releaseYear: 0,
            ifHQ: "",
            tracks: undefined //tracks here
        };
        album[i].artistName = prompt("What is the artist's name?");
        album[i].albumTitle = prompt("What is the album title?");
        album[i].releaseYear = parseFloat(prompt("What is the album's release year"));
        album[i].ifHQ = prompt("Is the album high quality? Y/N");

        while (!(album[i].ifHQ === "Y" || album[i].ifHQ === "N" || album[i].ifHQ === "YES" || album[i].ifHQ === "NO")) {
            album[i].ifHQ = prompt("You have entered an invalid response. Is " + album[i].title + " a ifHQ album, Y/N?");
            album[i].ifHQ = album[i].ifHQ.toUpperCase();
        }

        if (album[i].ifHQ === "Y") {
            album[i].ifHQ = true;
        }
        else {
            album[i].ifHQ = false;
        }
        album[i].tracks = new albumay(10);

        for (var j = 0 + 1; j < album[i].tracks.length; j++) {
            album[i].tracks[j] = prompt("Enter track" + (j + 1));


        }
    }
    for (var key in album[0]) {
        document.write(key + ": " + album[0][key] + " ");
        document.write(BR);
    }
    for (var key in album[1]) {
        document.write(key + ": " + album[1][key] + " ");
        document.write(BR);
    }

}

var same = compare(album[0], album[1]);
document.write(same);

// This line calls main, don't change it:
main();

编辑代码:

<script type="text/javascript">
            /********* GLOBAL VARIABLES *********/
            var BR = "<br />";
            var ES = " ";
            var album = [];

            /********* FUNCTIONS *********/
            function compare(album1, album2)
            {
                for (var i in album1)
                {
                    if (album1[i] !== album2[i]) return false;
                }
                return true;
            }

            /********* MAIN *********/
            function main()
            {

                var album = [];
                var numAlbums = 3;

                for (var i = 0; i < numAlbums; i++) {

                    album[i] = {
                        artistName: "",
                        albumTitle: "",
                        releaseYear: 0,
                        ifHQ: "",
                        tracks: undefined //tracks here
                    };
                    album[i].artistName = prompt("What is the artist's name?");
                    album[i].albumTitle = prompt("What is the album title?");
                    album[i].releaseYear = parseFloat(prompt("What is the album's release year"));
                    album[i].ifHQ = prompt("Is the album high quality? Y/N");

                    while (!(album[i].ifHQ === "Y" || album[i].ifHQ === "N" || album[i].ifHQ === "YES" || album[i].ifHQ === "NO"))
                    {
                        album[i].ifHQ = prompt("You have entered an invalid response. Is " + album[i].title + " a ifHQ album, Y/N?");
                        album[i].ifHQ = album[i].ifHQ.toUpperCase();
                    }

                    if (album[i].ifHQ === "Y")
                    {
                        album[i].ifHQ = true;
                    }
                    else
                    {
                        album[i].ifHQ = false;
                    }
                    album[i].tracks = new album(10);

                    for (var j = 0 + 1; j < album[i].tracks.length; j++)
                    {
                        album[i].tracks[j] = prompt("Enter track" + (j + 1));


                    }
                }
                for (var key in album[1])
                {
                    document.write(key + ": " + album[1][key] + " ");
                    document.write(BR);
                }
                for (var key in album[2])
                {
                    document.write(key + ": " + album[2][key] + " ");
                    document.write(BR);
                }
            }

            var same = compare(album1, album2);
            document.write(same);

            // This line calls main, don't change it:
            main();
        </script>
4

2 回答 2

0

如果您在代码上运行调试器,您会发现问题出在compare函数中。特别是,参数名称不能包含方括号。

我建议更改您的比较功能:

function compare (album1, album2) {
    for (var i in album1) {
        if (album1[i] !== album2[i]) return false;
    }
    return true;
}
于 2012-12-31T01:30:28.477 回答
0

根据您的更新

好的,这段代码仍然存在很多问题。

首先也是最重要的,在比较之前没有定义专辑 1 和专辑 2,原因有两个。

  1. Main() 直到它们运行后才会执行,因此尚未定义专辑 []
  2. 专辑 [1] 与专辑 1 不同

还有一些需要考虑的事情:

  1. 数组索引在 javascript 中从 0 开始。跳过第一个索引以使您的数组从 1 开始被认为是不好的做法。
  2. 而不是您现在正在执行的 24 个提示,您可能应该考虑输入表单。连续 24 个提示对用户来说非常烦人。
  3. 您可能需要一个用于 HQ 验证的模式窗口。像这样的东西

清理代码的尝试

我认为您可能应该重新考虑这里的一些选择,但这是至少运行的代码的清理版本:

jsfiddle

<script type="text/javascript">

    /********* FUNCTIONS *********/
    function compare(album1, album2)
    {
        for (var i in album1)
        {
            if (album1[i] !== album2[i]) return false;
        }
        return true;
    }

    /********* MAIN *********/
    function main()
    {

        var album = [];
        var numAlbums = 2;

        for (var i = 0; i < numAlbums; i++) {

            album[i] = {};
            album[i].artistName = prompt("What is the artist's name?");
            album[i].albumTitle = prompt("What is the album title?");
            album[i].releaseYear = parseFloat(prompt("What is the album's release year"));
            var hq = prompt("Is the album high quality? Y/N")
            if(!hq)
            {
                hq = "";
            }

            while (true)
            {
                hq = hq.toUpperCase();
                if( hq==="Y" || hq === "YES" )
                {
                    album[i].ifHQ = true;
                    break;
                }
                else if( hq ==="N" || hq === "NO" )
                {
                    album[i].ifHQ = false;
                    break;
                }
                else
                {
                hq = prompt(
                    "You have entered an invalid response. Is " + 
                    album[i].title + " a ifHQ album, Y/N?");
                }
            }
            album[i].tracks = [];

            for (var j = 0; j < 10; j++)
            {
                album[i].tracks[j] = prompt("Enter track" + (j + 1));
            }
        }

        var same = compare(album[0], album[1]);
        document.write(same);
    }
    // This line calls main, don't change it:
    main();
</script>
于 2012-12-31T01:50:07.910 回答