Can't Figure Out How To Write Loop

I have this program I’m writing, and I can’t figure out how to write the loop. The Pseudocode is in the code. Any suggestions would be appreciated… Here is the code.

/** 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 ti 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 object
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);
    function purchase(){
        quantity--;
        if (quantity > 0){
            return 1;
        }
        else{
            return -1;
        }
    };
};
// Create classes that inherit from Album
var pimpAButterfly = new Album("To Pimp a Butterfly", "Kendrick Lamar", 19.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", 10.83);
killLights.tracklisting = ["Kick the Dust Up", "Kill the Lights", "Strip it Down", "Home Alone Tonight"];

var platinum = new Album("Platinum", "Miranda Lambert", 10.61);
platinum.tracklisting = ["Girls", "Platinum", "Little Red Wagon", "Priscilla", "Automatic"];

var painKiller = new Album("PainKiller", "Little Big Town", 10.61);
painKiller.tracklisting = ["Quit Breaking Up With Me", "Day Drinking", "Tumble and Fall", "Painkiller"];

// Store these classes in an array
var albums = [pimpAButterfly, blameItAll, killLights, platinum, painKiller];

// Create a constructor for Cart object
function Cart(){
    this.items = [];
    function add(item){
        this.items.push(item);
    };
    function remove(){
        this.items.pop();
    };
};

// Create a constant variable for initial money
var INITIAL_MONEY = 1000.00;

// Create an instance of the Cart object
var cart = new Cart();



console.log("Album Name\tArtist Name\tQuantity\tSubtotal");

Maybe something like?

function canBuyAlbum (money, album) {
  return album.price <= money
}

function buyAlbum (money, album) {
  album.purchase()
  cart.add(album) 
  return money - album.price
}

function buyAlbums (money, albums) {
  if (!albums.length) return money
  var album = albums.pop()
  if (!canBuyAlbum(money, album)) return money
  return buyAlbums(buyAlbum(money, album), albums)
}

if you are using something like babel it will optimize the recursion for you, otherwise you will have to do something like this instead

function buyAlbums (money, albums) {
  __loop: while(1) {
    if (!albums.length) return money
    var album = albums.pop()
    if (!canBuyAlbum(money, album)) return money
    money = buyAlbum(money, album)
    continue __loop
  }
}
1 Like

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