我正在尝试创建一个功能,将一个音乐专辑与另一个音乐专辑进行比较但我的代码不会运行。我想知道为什么,因为我真的看不出问题出在哪里以及为什么会出现问题。非常感谢。
/********* 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>
答案 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。
我认为你应该重新考虑一下这里的一些选择,但这里是一个至少运行的代码的清理版本:
<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>