diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index 28b55bc..515414d 100644 --- a/Exercises/day-1/object-exercise-3.js +++ b/Exercises/day-1/object-exercise-3.js @@ -1,30 +1,30 @@ // Exercises: Level 3 // Create an object literal called personAccount. It has firstName, lastName, incomes, expenses properties and it has totalIncome, totalExpense, accountInfo,addIncome, addExpense and accountBalance methods. Incomes is a set of incomes and its description and expenses is a set of incomes and its description. -const personAccount = { - firstName: 'fitsum', - lastName: 'helina', - incomes: [ - { description: 'Salary', amount: 50000 }, - { description: 'Bonus', amount: 10000 }, - ], - expenses: [ - { description: 'Rent', amount: 10000 }, - { description: 'Groceries', amount: 5000 }, - ], - totalIncome() { - return this.incomes.reduce((total, income) => total + income.amount, 0); - }, - totalExpense() { - return this.expenses.reduce((total, expense) => total + expense.amount, 0); - }, - accountInfo() { - return `Account Information: ${this.firstName} ${this.lastName}`; - }, - addIncome(description, amount) { - this.incomes.push({ description, amount }); - }, -} +// const personAccount = { +// firstName: 'fitsum', +// lastName: 'helina', +// incomes: [ +// { description: 'Salary', amount: 50000 }, +// { description: 'Bonus', amount: 10000 }, +// ], +// expenses: [ +// { description: 'Rent', amount: 10000 }, +// { description: 'Groceries', amount: 5000 }, +// ], +// totalIncome() { +// return this.incomes.reduce((total, income) => total + income.amount, 0); +// }, +// totalExpense() { +// return this.expenses.reduce((total, expense) => total + expense.amount, 0); +// }, +// accountInfo() { +// return `Account Information: ${this.firstName} ${this.lastName}`; +// }, +// addIncome(description, amount) { +// this.incomes.push({ description, amount }); +// }, +// } // **** Questions:2, 3 and 4 are based on the following two arrays:users and products () @@ -102,7 +102,28 @@ const products = [ ] // 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. - // b. Create a function called signIn which allows user to sign in to the application + const newuser = { + _id: 'abc123', + username: 'John Doe', + email: 'johndoe@johndoe.com', + password: '123456', + createdAt: '08/01/2020 10:15 AM', + isLoggedIn: false, + }; + const signUp = (newuser) => { + const userexist = users.find((user) => user._id === newuser._id) + if (userexist) { + console.log('User already exists!') + return; + } + else{ + users.push(newuser) + console.log('User added successfully!') + } + } + signUp(newuser); + + // b. Create a function called signIn which allows user to sign in to the application // The products array has three elements and each of them has six properties. // a. Create a function called rateProduct which rates the product