function exresice 1 90% complated

pull/420/head
Fitsumhelina 10 months ago
parent 11e5194dfc
commit 656beaa7b8

@ -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.

@ -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. // 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 ()
@ -111,32 +111,30 @@ const newuser = {
isLoggedIn: null, isLoggedIn: null,
}; };
const signUp = (newuser) => { const signUp = (newuser) => {
// //solution1 // //solution1
const userExists = users.find((user) => user._id === newuser._id); const userExists = users.find((user) => user._id === newuser._id);
if (userExists) { if (userExists) {
console.log('User already exists!') console.log("User already exists!");
return; return;
} } else {
else{ users.push(newuser);
users.push(newuser) console.log("User added successfully!");
console.log('User added successfully!') }
}
// solution 2 // solution 2
for (const user of users ){ for (const user of users) {
if(user._id === newuser._id){ if (user._id === newuser._id) {
console.log('User already exists!') console.log("User already exists!");
return; return;
} } else {
else{ users.push(newuser);
users.push(newuser) console.log("User added successfully!");
console.log('User added successfully!') break;
break;
}
}
} }
signUp(newuser); }
};
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
const signIn = (newuser) => { 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 // b. Create a function called averageRating which calculate the average rating of a product
const averageRating = (productid , products ) => { const averageRating = (productid, products) => {
const product = products.find ((item) => productid === item._id); const product = products.find((item) => productid === item._id);
if (product) { if (product) {
if (product.ratings.length === 0){ if (product.ratings.length === 0) {
console.log(`No ratings yet for ${product.name}`) console.log(`No ratings yet for ${product.name}`);
return; 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 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. // 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 likeProduct = (productId, userId, products) => {
const product = products.find((item) => productId === item._id); const product = products.find((item) => productId === item._id);
if (product) { if (product) {
const exixst = product.likes.find((like) => userId === like); const exixst = product.likes.find((like) => userId === like);
if (exixst) { if (exixst) {
product.likes = product.likes.filter((like) => like !== userId); product.likes = product.likes.filter((like) => like !== userId);
console.log(`${userId} has removed like from ${product.name}`); console.log(`${userId} has removed like from ${product.name}`);
} else {
product.likes.push(userId);
console.log(`${userId} has liked ${product.name}`);
}
} else { } else {
console.log("product not found"); product.likes.push(userId);
console.log(`${userId} has liked ${product.name}`);
} }
} } else {
console.log("product not found");
}
};

Loading…
Cancel
Save