Here is my code…
function isValidAlbum(i){
if (albums[i].quantity > 0 && albums[i].price < INITIAL_MONEY){
return true;
}
};
// Loop to simulate purchases
while (INITIAL_MONEY > 0){
for (var i = 0; i < albums.length; i++){
// Purchase each one as long as there is money left
albums.filter(isValidAlbum(i));
albums[i].purchase();
//Decrement money by purchase price
INITIAL_MONEY -= albums[i].price;
// Add item to cart
cart.add(albums[i]);
};
};
So what I’m trying to do is purchase an album as long as I have money left. INITIAL_MONEY starts at 1000.00. I have an array of 5 different albums, with various prices, and a random amount of particular albums. So I need to iterate through the array and purchase each album as long as there is money left to purchase the album, and the quantity of that particular album is > 0; if not, I need to be able to try and purchase the next album. The code above is close, but I’m not quite there. Any suggestions? I know I probably screwed up with my filter function, but I think filter is part of the puzzle.