Javascript(比较对象的功能)

时间:2012-12-31 01:20:47

标签: javascript function object

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

/********* 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>

2 个答案:

答案 0 :(得分:0)

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

我建议改变你的比较功能:

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

答案 1 :(得分:0)

根据您的更新

好的,这段代码仍有很多问题。

首先也是最重要的,由于2个原因,在比较之前没有定义album1和album2。

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

    还有一些事情要考虑:

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

      尝试清理代码

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

      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>