Managing Product Reviews with JavaScript

Introduction

Hello, and welcome to today's lesson! Today, we are going to dive into the world of managing product reviews and applying data aggregation in practice. We will start with a relatively simple Starter Task to set up our base and then gradually build up to a more complex solution involving data aggregation. Let's jump in!

Starter Task: Methods and Their Definitions

For our starter task, we will lay the foundation by implementing basic operations for managing product reviews. These are the methods we will need to implement:

  • addReview(productId, reviewId, reviewText, rating) — adds a review to the product specified by productId. If a review with reviewId already exists, it updates the existing review. The review contains a flagged field which indicates whether the review is marked as inappropriate (by default, this field is set to false). Returns true if the review was added or updated successfully, false otherwise.

  • getReview(productId, reviewId) — returns the review details (reviewText, rating, and flagged fields) for the review specified by reviewId under the given productId. If the review or product does not exist, returns null.

  • deleteReview(productId, reviewId) — deletes the review specified by reviewId under the given productId. Returns true if the review was deleted, false otherwise.

Starter Task Implementation

Let's look at the code that implements these functionalities:

JavaScript
class ReviewManager {
    constructor() {
        // Initialize an empty object to store product reviews.
        this.products = {};
    }

    addReview(productId, reviewId, reviewText, rating) {
        // Ensure the rating is between 1 and 5.
        if (rating < 1 || rating > 5) {
            return false; // Invalid rating
        }
        // If the product doesn't exist, create a new entry for it.
        if (!this.products[productId]) {
            this.products[productId] = {};
        }
        // Add or update the review for the product.
        this.products[productId][reviewId] = { text: reviewText, rating: rating, flagged: false };
        return true;
    }

    getReview(productId, reviewId) {
        // Check if the product and review exist.
        if (this.products[productId] && this.products[productId][reviewId]) {
            let review = this.products[productId][reviewId];
            // Return the review details.
            return { text: review.text, rating: review.rating, flagged: review.flagged };
        }
        return null;
    }

    deleteReview(productId, reviewId) {
        // Check if the product and review exist.
        if (this.products[productId] && this.products[productId][reviewId]) {
            // Delete the specified review.
            delete this.products[productId][reviewId];
            // If no reviews are left for the product, remove the product entry.
            if (Object.keys(this.products[productId]).length === 0) {
                delete this.products[productId]; // Remove product if no reviews are left
            }
            return true;
        }
        return false;
    }
}

// Instantiate the ReviewManager
const reviewManager = new ReviewManager();

// Adding some reviews
reviewManager.addReview("p1", "r1", "Great product!", 5);
reviewManager.addReview("p1", "r2", "Not bad", 3);

// Testing getReview method
console.log(reviewManager.getReview("p1", "r1"));  // Expected: { text: "Great product!", rating: 5, flagged: false }
console.log(reviewManager.getReview("p1", "r3"));  // Expected: null

// Testing deleteReview method
console.log(reviewManager.deleteReview("p1", "r2"));  // Expected: true
console.log(reviewManager.getReview("p1", "r2"));  // Expected: null

This code establishes the foundational methods needed for managing product reviews within a ReviewManager class. The addReview method allows for adding a new review or updating an existing one, ensuring each review contains valid rating values between 1 and 5. The getReview method retrieves the review details for a specific product, including the review text and rating, returning null if the product or review doesn't exist. The deleteReview method facilitates the removal of a specific review, and if no reviews are left for a product, the product itself is removed from the product list. Together, these methods form the basic operations required to manage a collection of product reviews efficiently.

Now, let's extend this with new features.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal