diff --git a/Exercises/day-1/function-exersice-1.js b/Exercises/day-1/function-exersice-1.js new file mode 100644 index 0000000..55676ae --- /dev/null +++ b/Exercises/day-1/function-exersice-1.js @@ -0,0 +1,30 @@ +// Exercises: Level 1 + +// Declare a function fullName and it takes firstName, lastName as a parameter and it returns your full - name. +const fullname = (firstname, lastname) => { + return `${firstname} ${lastname}` +} +// Declare a function addNumbers and it takes two two parameters and it returns sum. +const sum = (a, b) =>{ + return a + b +} + +// Area of a circle is calculated as follows: area = π x r x r. Write a function which calculates _areaOfCircle +const areaOfCircle = (r,pi=3.14) => { + return pi * r * r +} + +// Temperature in oC can be converted to oF using this formula: oF = (oC x 9/5) + 32. Write a function which convert oC to oF convertCelciusToFahrenheit. +const temperature = (oC) => { + return (oC * 9/5) + 32 +} + +// Body mass index(BMI) is calculated as follows: bmi = weight in Kg / (height x height) in m2. Write a function which calculates bmi. BMI is used to broadly define different weight groups in adults 20 years old or older.Check if a person is underweight, normal, overweight or obese based the information given below. + + // The same groups apply to both men and women. + // Underweight: BMI is less than 18.5 + // Normal weight: BMI is 18.5 to 24.9 + // Overweight: BMI is 25 to 29.9 + // Obese: BMI is 30 or more + +// Write a function called checkSeason, it takes a month parameter and returns the season:Autumn, Winter, Spring or Summer. \ No newline at end of file diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index f080f07..ce68683 100644 --- a/Exercises/day-1/object-exercise-3.js +++ b/Exercises/day-1/object-exercise-3.js @@ -2,29 +2,29 @@ // 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 }); - }, -} + 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 () @@ -111,32 +111,30 @@ const newuser = { isLoggedIn: null, }; - const signUp = (newuser) => { -// //solution1 - const userExists = users.find((user) => user._id === newuser._id); - if (userExists) { - console.log('User already exists!') - return; - } - else{ - users.push(newuser) - console.log('User added successfully!') - } +const signUp = (newuser) => { + // //solution1 + const userExists = users.find((user) => user._id === newuser._id); + if (userExists) { + console.log("User already exists!"); + return; + } else { + users.push(newuser); + console.log("User added successfully!"); + } -// solution 2 - for (const user of users ){ - if(user._id === newuser._id){ - console.log('User already exists!') - return; - } - else{ - users.push(newuser) - console.log('User added successfully!') - break; - } - } + // solution 2 + for (const user of users) { + if (user._id === newuser._id) { + console.log("User already exists!"); + return; + } else { + users.push(newuser); + console.log("User added successfully!"); + break; } - signUp(newuser); + } +}; +signUp(newuser); // b. Create a function called signIn which allows user to sign in to the application const signIn = (newuser) => { @@ -196,36 +194,37 @@ const rateproduct = (productId, userid, rate) => { } }; // b. Create a function called averageRating which calculate the average rating of a product -const averageRating = (productid , products ) => { - const product = products.find ((item) => productid === item._id); - if (product) { - if (product.ratings.length === 0){ - console.log(`No ratings yet for ${product.name}`) - return; - } - const sum = product.ratings.reduce((sum,rating) => sum + rating.rate ,0) - const average = sum / product.ratings.length - console.log(`average rating for ${product.name} is ${average} and it had ${product.ratings.length} rates `) - } - else { - console.log("product not found"); +const averageRating = (productid, products) => { + const product = products.find((item) => productid === item._id); + if (product) { + if (product.ratings.length === 0) { + console.log(`No ratings yet for ${product.name}`); + return; } -} + const sum = product.ratings.reduce((sum, rating) => sum + rating.rate, 0); + const average = sum / product.ratings.length; + console.log( + `average rating for ${product.name} is ${average} and it had ${product.ratings.length} rates ` + ); + } else { + console.log("product not found"); + } +}; // 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. const likeProduct = (productId, userId, products) => { - const product = products.find((item) => productId === item._id); - if (product) { - const exixst = product.likes.find((like) => userId === like); - if (exixst) { - product.likes = product.likes.filter((like) => like !== userId); - console.log(`${userId} has removed like from ${product.name}`); - } else { - product.likes.push(userId); - console.log(`${userId} has liked ${product.name}`); - } + const product = products.find((item) => productId === item._id); + if (product) { + const exixst = product.likes.find((like) => userId === like); + if (exixst) { + product.likes = product.likes.filter((like) => like !== userId); + console.log(`${userId} has removed like from ${product.name}`); } else { - console.log("product not found"); + product.likes.push(userId); + console.log(`${userId} has liked ${product.name}`); } -} \ No newline at end of file + } else { + console.log("product not found"); + } +};