document.write を使用して、すべてのオブジェクト プロパティをコピーしてオブジェクト化し、出力する関数を作成する必要があります。
このような関数を作成したのですが、結果として [object Object] が返され、理由がわかりません。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
/********* GLOBAL VARIABLES *********/
var BR = "<br />";
var ES = " ";
/********* FUNCTIONS *********/
function compare(album0, album1) {
//This seems to do exactly what's needed... but sadly I dont have time to test every possible scenario.
var sameTracks = true;
for (var i = 0; i < album0.length; i++) {
if (!(album0.tracks[i] === album1.tracks[i])) sameTracks = false;
}
for (var i = 0; i < album1.length; i++) {
if (!(album0.tracks[i] === album1.tracks[i])) sameTracks = false;
}
if (
album0.artistName === album1.artistName && album0.albumTitle === album1.albumTitle && album0.releaseYear === album1.releaseYear && album0.ifHQ === album1.ifHQ && album0.tracks.length === album1.tracks.length && sameTracks === true) {
return true
}
else {
return false
};
}
function copy(album1) //the copy function
{
//properties will be copied over from original album1 into copy
var albumCopy =
{
artistName: "",
albumTitle: "",
releaseYear: 0,
ifHQ: "",
tracks: undefined
};
//copies original object values item for item into duplicate album
for (var key in albumCopy)
{
albumCopy[key] = album1[key];
}
return albumCopy;
}
/********* MAIN *********/
function main() {
var album = new Array(2)
for (var i = 0; i < album.length; i++) //ok, so basically this loop is responsible for creating the album objects...
{
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")) //to handle bad input
{
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 Array(2); // the tracks property of each album object is an array of tracks...
for (var j = 0; j < album[i].tracks.length; j++) {
album[i].tracks[j] = prompt("Enter track " + (j + 1));
}
}
for (var key in album[0]) //and these for... in loops navigate through properties for these objects to output what was stored in them
{
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("The status of both albums being identical is" + ES + same + BR);
var copiedAlbum = copy(album[1]);
document.write(copiedAlbum);
}
// This line calls main, don't change it:
main();
</script>
</body>