/
Write
Start a team blog
NEW ✨
Start a team blog, invite your team, and start publishing.

How come the condition is not working?

Kristy Lee's photo
Kristy Lee
·Jun 18, 2020

I am given the products variable and need to create a function called rateProduct which rates the product, but if you've already rated that product you don't get to rate again. Now my code seems to be working however when I add my username the second time, I can still rate the same product again and again but the hardcoded userIds (fg12cy and zwf8md) would not be able to rate the same product again and again. When I console log the variable product, I see my personal username is added the first time into product, so why is it not following the condition coded like the hardcoded userIds?

const products = [
  {
    _id: 'eedfcf',
    name: 'mobile phone',
    description: 'Huawei Honor',
    price: 200,
    ratings: [
      { userId: 'fg12cy', rate: 5 },
      { userId: 'zwf8md', rate: 4.5 }
    ],
    likes: []
  },
  {
    _id: 'aegfal',
    name: 'Laptop',
    description: 'MacPro: System Darwin',
    price: 2500,
    ratings: [],
    likes: ['fg12cy']
  },
  {
    _id: 'hedfcg',
    name: 'TV',
    description: 'Smart TV:Procaster',
    price: 400,
    ratings: [{ userId: 'fg12cy', rate: 5 }],
    likes: ['fg12cy']
  }
  ]

function rateProduct (description, userId, rate){
    let correctDescription = true
    let ratingObject = {}
    ratingObject.userId = userId
    ratingObject.rate = rate
    let alert = true
    products.forEach((item) => {
      if(description === item.description){
        correctDescription = true;
        for(let i=0; i<item.ratings.length; i++){
          if(userId !== item.ratings[i].userId){
            alert = false
            item.ratings.push(ratingObject)
            return 
          } 
        } 
      } else{
        correctDescription = false
      } 
    })
    if(!correctDescription){
      return "No product is found to be rated"
    } else if(alert) {
      return "You've already rated this product"
    }
    return products
}
console.log(rateProduct("Smart TV:Procaster", "fg12cy", 4))
console.log(rateProduct("Smart TV:Procaster", "kris", 4)) /* initially updated product with "kris" and 4, but after calling the function again with "kris" and 3, the product here also got updated with "kris" and 3 - not staying as "kris" 
and 4 here for ratings inside product*/
console.log(rateProduct("Smart TV:Procaster", "kris", 3)) 
console.log(rateProduct("Smart TV:Procaster", "zwf8md", 2))