@ -1,30 +1,30 @@
// Exercises: Level 3
// 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.
// 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 = {
// const personAccount = {
firstName : 'fitsum' ,
// firstName: 'fitsum',
lastName : 'helina' ,
// lastName: 'helina',
incomes : [
// incomes: [
{ description : 'Salary' , amount : 50000 } ,
// { description: 'Salary', amount: 50000 },
{ description : 'Bonus' , amount : 10000 } ,
// { description: 'Bonus', amount: 10000 },
] ,
// ],
expenses : [
// expenses: [
{ description : 'Rent' , amount : 10000 } ,
// { description: 'Rent', amount: 10000 },
{ description : 'Groceries' , amount : 5000 } ,
// { description: 'Groceries', amount: 5000 },
] ,
// ],
totalIncome ( ) {
// totalIncome() {
return this . incomes . reduce ( ( total , income ) => total + income . amount , 0 ) ;
// return this.incomes.reduce((total, income) => total + income.amount, 0);
} ,
// },
totalExpense ( ) {
// totalExpense() {
return this . expenses . reduce ( ( total , expense ) => total + expense . amount , 0 ) ;
// return this.expenses.reduce((total, expense) => total + expense.amount, 0);
} ,
// },
accountInfo ( ) {
// accountInfo() {
return ` Account Information: ${ this . firstName } ${ this . lastName } ` ;
// return `Account Information: ${this.firstName} ${this.lastName}`;
} ,
// },
addIncome ( description , amount ) {
// addIncome(description, amount) {
this . incomes . push ( { description , amount } ) ;
// this.incomes.push({ description, amount });
} ,
// },
}
// }
// **** Questions:2, 3 and 4 are based on the following two arrays:users and products ()
// **** Questions:2, 3 and 4 are based on the following two arrays:users and products ()
@ -102,6 +102,27 @@ const products = [
]
]
// 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 = {
_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
// 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.
// The products array has three elements and each of them has six properties.