Reputation: 1
I'm having trouble displaying the title of each album in an alert when the page opens. I am not sure what I am doing wrong with the access of the array in the for loop. Any clues?
I apologize if I am new to this and this seems like an obvious solution. I have to use JavaScript too, please keep in mind. I am just wondering if I am completely accessing the indexes in the for loop the wrong way.
Is it the command after the for loop that is incorrect?
var fakeDatabase = [];
var foxyShazam = {
id: foxyShazam_FS,
title:'Foxy Shazam',
artist:'Foxy Shazam',
price: '$14.99',
releaseDate: new Date(1968, 10, 22),
Quantity: 50,
Trackinglist: ["Intro: Bombs Away", "Wanna-Be Angel", "Count Me Out", "Bye Bye Symphony", "Unstoppable", "Second Floor", "Oh Lord", "Connect", "The Only Way to My Heart...", "Killin' It", "Evil Thoughts"]
};
var thriller = {
id: thriller_MJ,
title:'Thriller',
artist:'Michael Jackson',
price: '$12.99',
releaseDate: new Date(1982, 10, 30),
Quantity: 35,
Trackinglist: ["Wanna Be Startin Somethin", "Baby Be Mine", "The Girl Is Mine", "Thriller", "Beat It", "Billie Jean", "Human Nature", "P.Y.T. (Pretty Young Thing)", "The Lady in My Life"]
};
var millennium = {
id: millennium_BSB,
title:'Millennium',
artist:'Backstreet Boys',
price: '$7.99',
releaseDate: new Date(1999, 4, 18),
Quantity: 15,
Trackinglist: ["Larger Than Life", "I Want It That Way", "Show Me the Meaning", "It's Gotta Be You", "I Need You Tonight", "Don't Want You Back", "Don't Wanna Lose You Now", "The One", "Back to Your Heart", "Spanish Eyes", "No One Else Comes Close", "The Perfect Fan"]
};
var darkSideOfTheMoon = {
id: darkSideOfTheMoon_PinkFloyd,
title:'Dark Side of the Moon',
artist:'Pink Floyd',
price: '$14.99',
releaseDate: new Date(1973, 02, 01),
Quantity: 60,
Trackinglist: ["Speak to Me", "Breathe", "On the Run", "Time", "The Great Gig in the Sky", "Money", "Us and Them", "Any Colour You Like", "Brian Damage", "Eclipse"]
};
fakeDatabase.push(whiteAlbum_Beatles, thriller_MJ, millennium_BSB, darkSideOfTheMoon_PinkFloyd);
function displayAlbum() {
for (var i=0; i < fakeDatabase.length; i++) {
alert(title);}
};
displayAlbum()
Upvotes: 0
Views: 48
Reputation: 974
Just couple of correction and your code works:
Here you go :)
<script type="text/javascript">
var fakeDatabase = [];
var foxyShazam = {
id: "foxyShazam_FS",
title:'Foxy Shazam',
artist:'Foxy Shazam',
price: '$14.99',
releaseDate: new Date(1968, 10, 22),
Quantity: 50,
Trackinglist: ["Intro: Bombs Away", "Wanna-Be Angel", "Count Me Out", "Bye Bye Symphony", "Unstoppable", "Second Floor", "Oh Lord", "Connect", "The Only Way to My Heart...", "Killin' It", "Evil Thoughts"]
};
var thriller = {
id: "thriller_MJ",
title:'Thriller',
artist:'Michael Jackson',
price: '$12.99',
releaseDate: new Date(1982, 10, 30),
Quantity: 35,
Trackinglist: ["Wanna Be Startin Somethin", "Baby Be Mine", "The Girl Is Mine", "Thriller", "Beat It", "Billie Jean", "Human Nature", "P.Y.T. (Pretty Young Thing)", "The Lady in My Life"]
};
var millennium = {
id: "millennium_BSB",
title:'Millennium',
artist:'Backstreet Boys',
price: '$7.99',
releaseDate: new Date(1999, 4, 18),
Quantity: 15,
Trackinglist: ["Larger Than Life", "I Want It That Way", "Show Me the Meaning", "It's Gotta Be You", "I Need You Tonight", "Don't Want You Back", "Don't Wanna Lose You Now", "The One", "Back to Your Heart", "Spanish Eyes", "No One Else Comes Close", "The Perfect Fan"]
};
var darkSideOfTheMoon = {
id: "darkSideOfTheMoon_PinkFloyd",
title:'Dark Side of the Moon',
artist:'Pink Floyd',
price: '$14.99',
releaseDate: new Date(1973, 02, 01),
Quantity: 60,
Trackinglist: ["Speak to Me", "Breathe", "On the Run", "Time", "The Great Gig in the Sky", "Money", "Us and Them", "Any Colour You Like", "Brian Damage", "Eclipse"]
};
fakeDatabase.push(foxyShazam, thriller, millennium, darkSideOfTheMoon);
function displayAlbum() {
for (var i=0; i < fakeDatabase.length; i++) {
alert(fakeDatabase[i].title);}
};
displayAlbum()
</script>
cheers, Ashok
Upvotes: 0
Reputation: 68616
First, you had some syntax errors - such as object ID's not being wrapped in quotes and an element referenced in the array that didn't exist (there wasn't a whiteAlbum_Beatles
object/ID mentioned in your code?)
I think you wanted to store the objects themselves in your array, not the object ID's.
You could use the following:
fakeDatabase.push(foxyShazam, thriller, millennium, darkSideOfTheMoon);
function displayAlbum() {
for (var i=0; i < fakeDatabase.length; i++) {
alert(fakeDatabase[i].title);
}
};
P.S: for future reference, remember it's better to test using console.log()
.
Upvotes: 1