From aaf7f36556c98a1c315b63b4f30697fd2178f413 Mon Sep 17 00:00:00 2001 From: kersys Date: Mon, 15 Jan 2024 14:18:40 +0000 Subject: [PATCH] Completed level 2 of the object exercises Almost completed level 3 --- .../Object Exercises/level2.js | 160 ++++++++++++------ .../Object Exercises/level3.js | 160 ++++++++++++++++++ 2 files changed, 267 insertions(+), 53 deletions(-) create mode 100644 01_Day_JavaScript_Refresher/01_javascript_refresher/Object Exercises/level3.js diff --git a/01_Day_JavaScript_Refresher/01_javascript_refresher/Object Exercises/level2.js b/01_Day_JavaScript_Refresher/01_javascript_refresher/Object Exercises/level2.js index e9d35bc..57e3fb1 100644 --- a/01_Day_JavaScript_Refresher/01_javascript_refresher/Object Exercises/level2.js +++ b/01_Day_JavaScript_Refresher/01_javascript_refresher/Object Exercises/level2.js @@ -1,63 +1,117 @@ 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 - } + 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 + } } function mostSkills() { let obj = undefined; for (const person in users) { - if (users.person.skills.length > obj.skills.length) { - obj = users.person; + if (obj == undefined) { + obj = users[person]; + continue; + } + if (users[person].skills.length > obj.skills.length) { + obj = users[person]; } } - console.log(obj); + return obj; +} + +function enoughPoints() { + let count = 0; + for (const person in users) { + if (Object.hasOwnProperty.call(users, person)) { + if (users[person].points >= 50) { + count += 1; + } + } + } + return count; +} + +function findMERN() { + const MERN = ["MongoDB", "Express", "React", "Node"]; + const people = {}; + for (const person in users) { + if (Object.hasOwnProperty.call(users, person) && MERN.every(stack => users[person].skills.includes(stack))) { + people[person] = users[person]; + } + } + return people; +} + +function addUser(user) { + let name = user.email; + name = name.split("@")[0]; + name = name[0].toUpperCase() + name.slice(1); + users[name] = user; +} + +function getKeys() { + const keys = []; + for (const person in users) { + if (Object.hasOwnProperty.call(users, person)) { + keys.push(person); + } + } + return keys; +} + +function getValues() { + const values = []; + for (const person in users) { + if (Object.hasOwnProperty.call(users, person)) { + values.push(users[person]); + } + } + return values; } -mostSkills(); \ No newline at end of file +// no countries object??? \ No newline at end of file diff --git a/01_Day_JavaScript_Refresher/01_javascript_refresher/Object Exercises/level3.js b/01_Day_JavaScript_Refresher/01_javascript_refresher/Object Exercises/level3.js new file mode 100644 index 0000000..8be2c33 --- /dev/null +++ b/01_Day_JavaScript_Refresher/01_javascript_refresher/Object Exercises/level3.js @@ -0,0 +1,160 @@ +const personAccount = { + firstName: "Joe", + lastName: "Dover", + incomes: [0], + expenses: [0], + + totalIncome: function() { + return this.incomes.reduce((acc, val) => acc+val); + }, + + totalExpense: function() { + return this.expenses.reduce((acc, val) => acc+val); + }, + + accountInfo: function() { + return `Name: ${this.lastName} ${this.firstName} In/Out: ${this.totalIncome()}/${this.totalExpense()}`; + }, + + addExpense: function(expense) { + this.expenses.push(expense); + }, + + accountBalance: function() { + return this.totalIncome() - this.totalExpense(); + } +} + +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, + }, +] +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 signUp(username, email, password) { + if (accountExists(email)) { + console.log("This account already exists"); + return; + } + const newUser = {}; + newUser._id = userIdGenerator(); + newUser.username = username; + newUser.email = email; + newUser.password = password; + newUser.createdAt = showDateTime(); + isLoggedIn = false; +} + +function showDateTime() { + const format = { + year: "numeric", + month: "2-digit", + day: "2-digit", + hour: "2-digit", + minute: "2-digit" + }; + console.log(Intl.DateTimeFormat(undefined, format).format(new Date()).replace(',', '')); +} + +function userIdGenerator() { + const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + result = ""; + for (let i = 0; i < 7; i++) { + result += chars[Math.floor(Math.random() * chars.length)]; + } + console.log(result); + return result; +} + +/** + * Checks if the account already exists + * returns boolean + */ +function accountExists(email) { + users.forEach(user => { + if (user.email == email) { + return true; + } + }); + return false; +} + +function rateProduct(userID, productID, rating) { + products.forEach(product => { + if (product._id == productID) { + product.ratings.push({userId: userID, rate: rating}); + return; + } + }); + console.log("There is no such product"); +} + +function averageRating() { + +} \ No newline at end of file