I really need help with this!

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.

You have two loops there nested one inside the other. The inner - for - loop purechases all the albums and then exits to the outer loop to test if there is any money left after purchasing all of them. If there is then it runs the loop to purchase them all again.

From the description of what you are trying to do there should only be one loop.

So I should get rid of my while loop?

I have to have to loops. I need to have a while loop to keep reiterating my for loop until my money is less than all of the albums price value or there are no more albums to purchase

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.

To do what you want you need to use only one loop. There are two termination tests for the loop - either the money has run out or there are no more albums to buy. You handle that by testing for both conditions in the same loop, not by making a separate loop for each.

var i = 0;
while(INITIAL_MONEY > 0 && i < albums.length){
         //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]);
        }
        i++;
};

Yeah, but wont that only iterate through the loop once? What if I still have money left over or there are still albums left to buy?

@felgall I did what you suggested and when I console.log INITIAL_MONEY, I get NaN.

/** 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
var i = 0;
while(INITIAL_MONEY > 0 && i < albums.length){
    //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.price;
        //Add item to cart
        cart.add(albums[i]);
    }
    i++;
}
console.log(INITIAL_MONEY);
//console.log("Album Name\tArtist Name\tQuantity\tSubtotal");**/

You’ve left out the array index. It should read:

INITIAL_MONEY = INITIAL_MONEY - albums[i].price;

@fretburner Thank you. That fixed it. As I was trying to explain to Felgal, I need to iterate until my money is spent or there is no more albums to buy. The loop that I used only iterates through one time. I can’t figure out how to loop through the array until the money is spent or albums are gone.

Those are the two termination conditions on the one loop I gave you - money all spent or no more albums - the loop will run until one or other of those conditions is met (or rather while neither is met - which is the same thing)…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.