Here is the whole program
/** Program Description: This program simulates purchases using a
* loop. The program creates two classes; an album class, and
* a cart class. The initial amount of money is $1000.00. The program
* iterates through all the albums and purchases each one, as
* long as there is money left. Every time an album gets purchased
* the initial amount of money is decremented by the purchase
* price. Each time an item is purchased, it gets added to the cart.
* The program then displays all the items purchased in the cart.
* It should show the album name, artist name, quantity purchased,
* and sub total for each. It then shows the total purchase price,
* and the amount of money left over
*
* Pseudocode:
*
* Create a constructor for Album object
* Create classes that inherit from Album
* Store these classes in an array
* Create a constructor for Cart object
* Create a const variable for initial money
* Loop to simulate purchases
* Iterate over an array of albums
* Purchase each one as long as there is money left
* Decrement money by purchase price
* Add item to cart
* Display info **/
// Create a constructor for Album class
function Album(title, artist, price){
this.title = title;
this.artist = artist;
this.price = price;
this.date = new Date();
this.quantity = Math.floor((Math.random() * 10) + 1);
};
Album.prototype.purchase = function(){
this.quantity--;
if (this.quantity > 0){
return 1;
}
else{
return -1;
}
};
// Create objects that inherit from Album
var pimpAButterfly = new Album("To Pimp a Butterfly", "Kendrick Lamar", 29.99);
pimpAButterfly.tracklisting = ["Wesleys Theory", "For Free", "King Kunta", "Institutionalized", "These Walls"];
var blameItAll = new Album("Blame It All On My Roots", "Garth Brooks", 29.98);
blameItAll.tracklisting = ["Blame It All On My Roots", "Neon Moon", "Papa", "Thunder Rolls"];
var killLights = new Album("Kill the Lights", "Luke Bryan", 20.83);
killLights.tracklisting = ["Kick the Dust Up", "Kill the Lights", "Strip it Down", "Home Alone Tonight"];
var platinum = new Album("Platinum", "Miranda Lambert", 20.61);
platinum.tracklisting = ["Girls", "Platinum", "Little Red Wagon", "Priscilla", "Automatic"];
var painKiller = new Album("PainKiller", "Little Big Town", 24.99);
painKiller.tracklisting = ["Quit Breaking Up With Me", "Day Drinking", "Tumble and Fall", "Painkiller"];
// Store these objects in an array
var albums = [pimpAButterfly, blameItAll, killLights, platinum, painKiller];
// Create a constructor for Cart class
function Cart(val){
this.items = [];
};
Cart.prototype.add = function(val){
this.items.push(val);
};
Cart.prototype.remove = function(val){
this.items.splice(albums.indexOf(val), 1);
};
//Create a constant variable for initial money
var INITIAL_MONEY = 1000.00;
// Create an instance of the Cart object
var cart = new Cart();
// 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
if(INITIAL_MONEY > albums[i].price){
albums[i].purchase();
//Decrement money by purchase price
INITIAL_MONEY = INITIAL_MONEY - albums[i].price;
//Add item to cart
cart.add(albums[i]);
}
};
};
console.log(INITIAL_MONEY);
//console.log("Album Name\tArtist Name\tQuantity\tSubtotal");**/
The problem is in my loop. I ran a console.log to check the initial money and the console is blank.