- Mar 26, 2005
- 4,090
- 119
- 106
Please take a look at my program. I cannot figure out what I am doing wrong. Code doesn't run.
I have two music albums that get populated with random info depending on user input. Then I have to compare if all the properties within two album objects are same.
I have two music albums that get populated with random info depending on user input. Then I have to compare if all the properties within two album objects are same.
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
/********* GLOBAL VARIABLES *********/
var BR = "<br />";
/********* 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;
}
/*function compareObject(album[0], album[1])
{
for (var key in album[0])
{
if (album[0].artistName !== album[1].artistName)
{
return false;
}
}
for (var key in album[1])
{
if (album[1].artistName !== album[0].artistName)
{
return false;
}
}
return true;
}
*/
/********* 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);
}
}
compare(album[0],album[1]);
// This line calls main, don't change it:
main();
</script>
</body>
</html>
Last edited: