object exercise

pull/98/head
nam dang tran 5 years ago
parent 6f863b0247
commit d8ee3a5910

@ -0,0 +1,211 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Object</title>
</head>
<body>
</body>
</html>
<script>
// const users = {
// Alex: {
// email: 'alex@alex.com',
// skills: ['HTML', 'CSS', 'JavaScript'],
// age: 20,
// isLoggedIn: false,
// points: 30
// },
// Asab: {
// email: 'asab@asab.com',
// skills: ['HTML', 'CSS', 'JavaScript', 'Redux', 'MongoDB', 'Express', 'React', 'Node'],
// age: 25,
// isLoggedIn: false,
// points: 50
// },
// Brook: {
// email: 'daniel@daniel.com',
// skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux'],
// age: 30,
// isLoggedIn: true,
// points: 50
// },
// Daniel: {
// email: 'daniel@alex.com',
// skills: ['HTML', 'CSS', 'JavaScript', 'Python'],
// age: 20,
// isLoggedIn: false,
// points: 40
// },
// John: {
// email: 'john@john.com',
// skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node.js'],
// age: 20,
// isLoggedIn: true,
// points: 50
// },
// Thomas: {
// email: 'thomas@thomas.com',
// skills: ['HTML', 'CSS', 'JavaScript', 'React'],
// age: 20,
// isLoggedIn: false,
// points: 40
// },
// Paul: {
// email: 'paul@paul.com',
// skills: ['HTML', 'CSS', 'JavaScript', 'MongoDB', 'Express', 'React', 'Node'],
// age: 20,
// isLoggedIn: false,
// points: 40
// }
// };
// //TODO: Find the person who has many skills in the users object.
// const copyUsers = Object.assign({},users);
// const usersKeys = Object.keys(copyUsers);
// const usersValues = Object.values(copyUsers);
// const usersEntries = Object.entries(copyUsers);
// // usersValues.forEach(value => {
// // console.log(value.skills)
// // });
// usersKeys.forEach(person => {
// // count logged in users, count users having greater than equal to 50 points from the following object.
// // if(copyUsers[person].points >= 50){
// // console.log(person);
// // }
// //Find people who are MERN stack developer from the users object
// const skills = copyUsers[person].skills;
// if(skills.includes('MongoDB') && skills.includes('Express') && skills.includes('React') && skills.includes('Node')){
// console.log("MERN stacker: "+ person);
// }
// });
// users.Nam = {
// email: 'nam@nam.com',
// skills: ['HTML', 'CSS', 'JavaScript', 'MongoDB', 'Express', 'React', 'Node'],
// age: 20,
// isLoggedIn: false,
// points: 100
// };
// Set your name in the users object without modifying the original users object
// console.log(users);
//Exercise lv3
const users = [
{
_id: 'ab12ex',
username: 'Alex',
email: 'alex@alex.com',
password: '123123',
createdAt: '08/01/2020 9:00 AM',
isLoggedIn: false,
},
{
_id: 'fg12cy',
username: 'Asab',
email: 'asab@asab.com',
password: '123456',
createdAt: '08/01/2020 9:30 AM',
isLoggedIn: true,
},
{
_id: 'zwf8md',
username: 'Brook',
email: 'brook@brook.com',
password: '123111',
createdAt: '08/01/2020 9:45 AM',
isLoggedIn: true,
},
{
_id: 'eefamr',
username: 'Martha',
email: 'martha@martha.com',
password: '123222',
createdAt: '08/01/2020 9:50 AM',
isLoggedIn: false,
},
{
_id: 'ghderc',
username: 'Thomas',
email: 'thomas@thomas.com',
password: '123333',
createdAt: '08/01/2020 10:00 AM',
isLoggedIn: false,
}
]
// .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.
function signUp(user) {
for (let i = 0; i < users.length; i++) {
const usr = users[i];
if (usr[username] === user[username]) {
console.log('The user is already existed');
break;
}
users.push(user);
};
}
function signIn(user) {
user.isLoggedIn = true;
}
signIn(users[4]);
console.log(users[4]);
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'],
},
]
// a. Create a function called rateProduct which rates the product
function rateProduct(userId, rate, proId) {
for (let i = 0; i < products.length; i++) {
const pro = products[i];
if (pro[_id] === proId) {
pro[ratings].push({ userId, rate });
break;
}
}
}
//b. Create a function called averageRating which calculate the average rating of a product
function likeProduct(userId, proId) {
for (let i = 0; i < products.length; i++) {
const pro = products[i];
if (pro[_id] === proId) {
pro[likes].push(userId);
break;
}
}
}
</script>
Loading…
Cancel
Save