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!
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 byproductId
. If a review withreviewId
already exists, it updates the existing review. The review contains aflagged
field which indicates whether the review is marked as inappropriate (by default, this field is set tofalse
). Returnstrue
if the review was added or updated successfully,false
otherwise. -
getReview(productId, reviewId)
— returns the review details (reviewText
,rating
, andflagged
fields) for the review specified byreviewId
under the givenproductId
. If the review or product does not exist, returnsnull
. -
deleteReview(productId, reviewId)
— deletes the review specified byreviewId
under the givenproductId
. Returnstrue
if the review was deleted,false
otherwise.
Let's look at the code that implements these functionalities:
JavaScript1class ReviewManager { 2 constructor() { 3 // Initialize an empty object to store product reviews. 4 this.products = {}; 5 } 6 7 addReview(productId, reviewId, reviewText, rating) { 8 // Ensure the rating is between 1 and 5. 9 if (rating < 1 || rating > 5) { 10 return false; // Invalid rating 11 } 12 // If the product doesn't exist, create a new entry for it. 13 if (!this.products[productId]) { 14 this.products[productId] = {}; 15 } 16 // Add or update the review for the product. 17 this.products[productId][reviewId] = { text: reviewText, rating: rating, flagged: false }; 18 return true; 19 } 20 21 getReview(productId, reviewId) { 22 // Check if the product and review exist. 23 if (this.products[productId] && this.products[productId][reviewId]) { 24 let review = this.products[productId][reviewId]; 25 // Return the review details. 26 return { text: review.text, rating: review.rating, flagged: review.flagged }; 27 } 28 return null; 29 } 30 31 deleteReview(productId, reviewId) { 32 // Check if the product and review exist. 33 if (this.products[productId] && this.products[productId][reviewId]) { 34 // Delete the specified review. 35 delete this.products[productId][reviewId]; 36 // If no reviews are left for the product, remove the product entry. 37 if (Object.keys(this.products[productId]).length === 0) { 38 delete this.products[productId]; // Remove product if no reviews are left 39 } 40 return true; 41 } 42 return false; 43 } 44} 45 46// Instantiate the ReviewManager 47const reviewManager = new ReviewManager(); 48 49// Adding some reviews 50reviewManager.addReview("p1", "r1", "Great product!", 5); 51reviewManager.addReview("p1", "r2", "Not bad", 3); 52 53// Testing getReview method 54console.log(reviewManager.getReview("p1", "r1")); // Expected: { text: "Great product!", rating: 5, flagged: false } 55console.log(reviewManager.getReview("p1", "r3")); // Expected: null 56 57// Testing deleteReview method 58console.log(reviewManager.deleteReview("p1", "r2")); // Expected: true 59console.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.
With our basic review management system in place, we will now introduce new methods to handle more complex operations, such as flagging inappropriate reviews and aggregating review data for a specific product.
Here are the new methods we will add:
-
flagReview(productId, reviewId)
— This method flags a specific review as inappropriate for a given product. Returnstrue
if the review was successfully flagged,false
otherwise. -
aggregateReviews(productId)
— This method aggregates review data for a given product, providing statistics such as the total number of reviews, the number of flagged reviews, average rating, and review texts excluding flagged ones. If the product does not have any reviews or does not exist, returnsnull
.
First, let's add functionality to flag a review:
JavaScript1class ReviewManager { 2 // Existing methods remain unchanged... 3 4 flagReview(productId, reviewId) { 5 // Check if the product and review exist. 6 if (this.products[productId] && this.products[productId][reviewId]) { 7 // Flag the review as inappropriate. 8 this.products[productId][reviewId].flagged = true; 9 return true; 10 } 11 return false; 12 } 13} 14 15// Instantiate the ReviewManager 16const reviewManager = new ReviewManager(); 17 18// Adding some reviews 19reviewManager.addReview("p1", "r1", "Great product!", 5); 20reviewManager.addReview("p1", "r2", "Not bad", 3); 21reviewManager.addReview("p1", "r3", "Terrible", 1); 22 23// Flagging a review 24reviewManager.flagReview("p1", "r3"); 25 26// Testing flagReview method 27console.log(reviewManager.getReview("p1", "r3")); // Expected: { text: "Terrible", rating: 1, flagged: true }
In this step, we are adding the flagReview
method to our ReviewManager
class. This method enables users to mark a specific review as inappropriate. It checks whether the product and review exist in the dataset, and if they do, it sets the flagged
attribute of the review to true
. This flagging mechanism is crucial for maintaining the quality and appropriateness of the reviews in the system.
Next, we will implement the method to aggregate reviews:
JavaScript1class ReviewManager { 2 // Existing methods remain unchanged... 3 4 flagReview(productId, reviewId) { 5 // Check if the product and review exist. 6 if (this.products[productId] && this.products[productId][reviewId]) { 7 // Flag the review as inappropriate. 8 this.products[productId][reviewId].flagged = true; 9 return true; 10 } 11 return false; 12 } 13 14 aggregateReviews(productId) { 15 // Check if the product exists and has reviews. 16 if (!this.products[productId] || Object.keys(this.products[productId]).length === 0) { 17 return null; // No reviews or product doesn't exist 18 } 19 20 // Initialize aggregation variables. 21 let totalReviews = Object.keys(this.products[productId]).length; 22 let flaggedReviews = 0; 23 let totalRating = 0; 24 let reviewTexts = []; 25 26 // Iterate through the product's reviews. 27 for (let reviewId in this.products[productId]) { 28 let review = this.products[productId][reviewId]; 29 if (review.flagged) { 30 flaggedReviews += 1; 31 } else { 32 totalRating += review.rating; 33 reviewTexts.push(review.text); 34 } 35 } 36 37 let averageRating = flaggedReviews === totalReviews ? 0 : totalRating / (totalReviews - flaggedReviews); 38 39 // Return the aggregated review data. 40 return { 41 totalReviews: totalReviews, 42 flaggedReviews: flaggedReviews, 43 averageRating: averageRating, 44 reviewTexts: reviewTexts 45 }; 46 } 47} 48 49// Instantiate the ReviewManager 50const reviewManager = new ReviewManager(); 51 52// Adding some reviews 53reviewManager.addReview("p1", "r1", "Great product!", 5); 54reviewManager.addReview("p1", "r2", "Not bad", 3); 55reviewManager.addReview("p1", "r3", "Terrible", 1); 56 57// Flagging a review 58reviewManager.flagReview("p1", "r3"); 59 60// Testing the aggregation method 61console.log(reviewManager.aggregateReviews("p1")); 62// Expected: 63// { 64// totalReviews: 3, 65// flaggedReviews: 1, 66// averageRating: 4, 67// reviewTexts: [ "Great product!", "Not bad" ] 68// }
In this step, the aggregateReviews
method is added to the ReviewManager
class. This method aggregates review data for a given product by calculating various statistics, such as the total number of reviews, the number of flagged reviews, the average rating excluding flagged reviews, and a list of review texts that are not flagged. The method first ensures the product exists and contains reviews. It then iterates through the reviews to collect the necessary data, considering only non-flagged reviews for the average rating and review texts. If all reviews are flagged, the average rating defaults to zero. This aggregation provides a comprehensive overview of a product’s review status, useful for both users and administrators.
Great job! Today, you have learned how to manage product reviews and apply data aggregation in practice. We started with basic operations for adding, retrieving, and deleting reviews. Then, we extended our functionality to include flagging reviews and aggregating review data. This gradual build-up demonstrates how to enhance features incrementally and handle more complex data aggregation tasks.
Feel free to practice solving similar challenges to strengthen your skills further. Keep coding, and see you in the next lesson!