CodeBlanks Logo Icon CodeBlanks

Listing the cart

Write the method that prints the cart formatted.

Listing the cart

To print the products in the cart we will write a printCart method. This method walks the this.items array and writes each product's name, price and quantity on one line.

You walk each product with for...of; on each iteration you print console.log(p.name + " " + p.price + " " + p.quantity);. Separating the three fields with spaces makes the output readable. The method returns no value; it only prints to the screen.

Formatted output

In printCart, while each product is written on a single line, spaces separate the fields. console.log(p.name + " " + p.price + " " + p.quantity) separates the three fields with spaces and ends with a new line.

Commas or dashes could be used instead; the formatting choice is yours. In this project we use spaces; for column alignment .padStart could fix the width. Writing three fields on one line lets you scan the cart quickly.

Practice Write the printCart method

Add a printCart method to the Cart class. Its signature must be printCart(). In the body walk each product with for (const p of this.items) and print with console.log(p.name + " " + p.price + " " + p.quantity);. main already adds two products and calls printCart. The output must be two lines: Notebook 25 2 and Pen 10 3.

Starter code

index.js
class Cart {
    constructor() {
        this.items = [];
    }
    addItem(product) {
        this.items.push(product);
    }
    getSubtotal() {
        let sum = 0;
        for (const p of this.items) {
            sum += p.price * p.quantity;
        }
        return sum;
    }
}
    // Write the printCart method here
}

// Test
const cart = new Cart();
cart.addItem({ name: "Notebook", price: 25, quantity: 2 });
cart.addItem({ name: "Pen", price: 10, quantity: 3 });
cart.printCart();

Sample solution

index.js
class Cart {
    constructor() {
        this.items = [];
    }
    addItem(product) {
        this.items.push(product);
    }
    getSubtotal() {
        let sum = 0;
        for (const p of this.items) {
            sum += p.price * p.quantity;
        }
        return sum;
    }
    printCart() {
        for (const p of this.items) {
            console.log(p.name + " " + p.price + " " + p.quantity);
        }
    }
}

// Test
const cart = new Cart();
cart.addItem({ name: "Notebook", price: 25, quantity: 2 });
cart.addItem({ name: "Pen", price: 10, quantity: 3 });
cart.printCart();

Expected output

Notebook 25 2
Pen 10 3

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does printCart do?

index.js
cart.printCart();
  • Removes a product
  • Computes the total
  • Prints the cart to the screen Correct answer
  • Adds a product

Explanation: printCart writes the products in the cart to the screen.

2 Write the printCart body

Question
printCart() {
for (const p of this.items) {
    console.log(____);
}
}
Answer
printCart() {
for (const p of this.items) {
    console.log(p.name + " " + p.price + " " + p.quantity);
}
}

Explanation: The three fields are joined with spaces.

3 Match each part to its meaning

Term Meaning
p.name Product name
p.price Unit price
p.quantity Quantity
console.log Prints to screen

All lessons in Beginner