product rating 100% complated

pull/420/head
Fitsumhelina 10 months ago
parent 721ad7cf91
commit ecbb5332a0

@ -30,86 +30,86 @@
const users = [ const users = [
{ {
_id: 'ab12ex', _id: "ab12ex",
username: 'Alex', username: "Alex",
email: 'alex@alex.com', email: "alex@alex.com",
password: '123123', password: "123123",
createdAt: '08/01/2020 9:00 AM', createdAt: "08/01/2020 9:00 AM",
isLoggedIn: false, isLoggedIn: false,
}, },
{ {
_id: 'fg12cy', _id: "fg12cy",
username: 'Asab', username: "Asab",
email: 'asab@asab.com', email: "asab@asab.com",
password: '123456', password: "123456",
createdAt: '08/01/2020 9:30 AM', createdAt: "08/01/2020 9:30 AM",
isLoggedIn: true, isLoggedIn: true,
}, },
{ {
_id: 'zwf8md', _id: "zwf8md",
username: 'Brook', username: "Brook",
email: 'brook@brook.com', email: "brook@brook.com",
password: '123111', password: "123111",
createdAt: '08/01/2020 9:45 AM', createdAt: "08/01/2020 9:45 AM",
isLoggedIn: true, isLoggedIn: true,
}, },
{ {
_id: 'eefamr', _id: "eefamr",
username: 'Martha', username: "Martha",
email: 'martha@martha.com', email: "martha@martha.com",
password: '123222', password: "123222",
createdAt: '08/01/2020 9:50 AM', createdAt: "08/01/2020 9:50 AM",
isLoggedIn: false, isLoggedIn: false,
}, },
{ {
_id: 'ghderc', _id: "ghderc",
username: 'Thomas', username: "Thomas",
email: 'thomas@thomas.com', email: "thomas@thomas.com",
password: '123333', password: "123333",
createdAt: '08/01/2020 10:00 AM', createdAt: "08/01/2020 10:00 AM",
isLoggedIn: false, isLoggedIn: false,
}, },
] ];
const products = [ const products = [
{ {
_id: 'eedfcf', _id: "eedfcf",
name: 'mobile phone', name: "mobile phone",
description: 'Huawei Honor', description: "Huawei Honor",
price: 200, price: 200,
ratings: [ ratings: [
{ userId: 'fg12cy', rate: 5 }, { userId: "fg12cy", rate: 5 },
{ userId: 'zwf8md', rate: 4.5 }, { userId: "zwf8md", rate: 4.5 },
], ],
likes: [], likes: [],
}, },
{ {
_id: 'aegfal', _id: "aegfal",
name: 'Laptop', name: "Laptop",
description: 'MacPro: System Darwin', description: "MacPro: System Darwin",
price: 2500, price: 2500,
ratings: [], ratings: [],
likes: ['fg12cy'], likes: ["fg12cy"],
}, },
{ {
_id: 'hedfcg', _id: "hedfcg",
name: 'TV', name: "TV",
description: 'Smart TV:Procaster', description: "Smart TV:Procaster",
price: 400, price: 400,
ratings: [{ userId: 'fg12cy', rate: 5 }], ratings: [{ userId: "fg12cy", rate: 5 }],
likes: ['fg12cy'], likes: ["fg12cy"],
}, },
] ];
// Imagine you are getting the above users collection from a MongoDB database. // Imagine you are getting the above users collection from a MongoDB database.
// a. Create a function called signUp which allows user to add to the collection. If user exists, inform the user that he has already an account. // a. Create a function called signUp which allows user to add to the collection. If user exists, inform the user that he has already an account.
const newuser = { const newuser = {
_id: 'eedfcf', _id: "eedfcf",
username: 'John Doe', username: "John Doe",
email: 'johndoe@johndoe.com', email: "johndoe@johndoe.com",
password: '123456', password: "123456",
createdAt: '08/01/2020 10:15 AM', createdAt: "08/01/2020 10:15 AM",
isLoggedIn: null, isLoggedIn: null,
}; };
// const signUp = (newuser) => { // const signUp = (newuser) => {
@ -139,68 +139,63 @@ const products = [
// } // }
// signUp(newuser); // signUp(newuser);
// b. Create a function called signIn which allows user to sign in to the application
// b. Create a function called signIn which allows user to sign in to the application const signIn = (newuser) => {
const signIn = (newuser) => { const exists = users.find((user) => user._id === newuser._id);
const exists = users.find(user => user._id === newuser._id); if (exists) {
if (exists){ exists.isLoggedIn = true;
exists.isLoggedIn = true; console.log("user signIn success");
console.log ("user signIn success") } else {
} exists.isLoggedIn = false;
else{ console.log("account does not exist");
exists.isLoggedIn = false; }
console.log ("account does not exist") };
}
}
// The products array has three elements and each of them has six properties. // The products array has three elements and each of them has six properties.
const product =[ const product = [
{ {
_id: 'eedfcf', _id: "eedfcf",
name: 'Mobile phone', name: "Mobile phone",
description: 'Huawei Honor', description: "Huawei Honor",
price: 200, price: 200,
ratings: [ ratings: [
{ userId: 'fg12cy', rate: 5 }, { userId: "fg12cy", rate: 5 },
{ userId: 'zwf8md', rate: 4.5 }, { userId: "zwf8md", rate: 4.5 },
], ],
likes: [], likes: [],
}, },
{ {
_id: 'aegfal', _id: "aegfal",
name: 'Laptop', name: "Laptop",
description: 'MacPro: System Darwin', description: "MacPro: System Darwin",
price: 2500, price: 2500,
ratings: [], ratings: [],
likes: ['fg12cy'], likes: ["fg12cy"],
}, },
{ {
_id: 'hedfcg', _id: "hedfcg",
name: 'TV', name: "TV",
description: 'Smart TV: Procaster', description: "Smart TV: Procaster",
price: 400, price: 400,
ratings: [{ userId: 'fg12cy', rate: 5 }], ratings: [{ userId: "fg12cy", rate: 5 }],
likes: ['fg12cy'], likes: ["fg12cy"],
}, },
] ];
// a. Create a function called rateProduct which rates the product // a. Create a function called rateProduct which rates the product
const rateproduct = (productId , userid , rate ) => { const rateproduct = (productId, userid, rate) => {
const product = product.find (item=>productId===item._id) const product = product.find((item) => productId === item._id);
if (product){ if (product) {
const exixst = product.ratings.find(rating => userid===rating.userId) const exixst = product.ratings.find((rating) => userid === rating.userId);
if(exixst){ if (exixst) {
console.log("user already rated this product") console.log("user already rated this product");
} } else {
else{ product.rating.push({ userid, rate });
product.rating.push({userid ,rate})
}
}
else{
console.log("product not found")
}
} }
// b. Create a function called averageRating which calculate the average rating of a product } else {
console.log("product not found");
}
};
// b. Create a function called averageRating which calculate the average rating of a product
// Create a function called likeProduct. This function will helps to like to the product if it is not liked and remove like if it was liked. // Create a function called likeProduct. This function will helps to like to the product if it is not liked and remove like if it was liked.
Loading…
Cancel
Save