From 0db687a7f5a043d1a94ec209a604c297287b7821 Mon Sep 17 00:00:00 2001 From: catsAndIvy Date: Wed, 5 Jul 2023 15:41:38 +0100 Subject: [PATCH] add solutions to day01 --- _solutions/day-01/exercise3/level3.js | 48 +++++++++++++++++++++++++++ _solutions/exercise4/level1.js | 22 ++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 _solutions/exercise4/level1.js diff --git a/_solutions/day-01/exercise3/level3.js b/_solutions/day-01/exercise3/level3.js index 3ef4e50..437f721 100644 --- a/_solutions/day-01/exercise3/level3.js +++ b/_solutions/day-01/exercise3/level3.js @@ -103,4 +103,52 @@ const signIn = function (email, password) { } }; +// c. Create a function called rateProduct which rates the product + +const rateProduct = function (productID, userID, rating) { + const foundProduct = products.find((product) => product._id === productID); + + if (foundProduct) { + foundProduct.ratings.push({userId: userID, rate: rating}) + console.log('Rating added successfully') + } else console.log('Product not found') +} + + +// d. Create a function called averageRating which calculate the average rating of a product + +const averageRating = function (productID) { + const foundProduct = products.find((product) => product._id === productID); + ratingsArr = [] + + if(foundProduct) { + foundProduct.ratings.forEach((rating) => { + ratingsArr.push(rating.rate) + }) + } else {console.log("Product not found.")} + + sum = ratingsArr.reduce(function (a, b) {return a + b;}, 0); + average = sum/ratingsArr.length + console.log(average) + return average +} + +// e. Create a function called likeProduct. This function will help us to like to the product if it is not liked and remove like if it was liked. + +const likeProduct = function (productID, userID) { + const foundProduct = products.find((product) => product._id === productID); + + if (foundProduct) { + if (foundProduct.likes.includes(userID)) { + const userIdIndex = foundProduct.likes.indexOf(userID); + foundProduct.likes.splice(userIdIndex, 1); // Remove the user ID from the 'likes' array + console.log("Product unliked."); + } else { + foundProduct.likes.push(userID); + console.log("Product liked."); + } + } else { + console.log("Product not found."); + } +} diff --git a/_solutions/exercise4/level1.js b/_solutions/exercise4/level1.js new file mode 100644 index 0000000..daa5ad2 --- /dev/null +++ b/_solutions/exercise4/level1.js @@ -0,0 +1,22 @@ +// Exercise 4 - Level 1 + +// 1. Declare a function fullName and it takes firstName, lastName as a parameter and it returns your full - name. + + + + +// 2. Declare a function addNumbers and it takes two two parameters and it returns sum. + +// 3. Area of a circle is calculated as follows: area = π x r x r. Write a function which calculates _areaOfCircle + +// 4. 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. + +// 5. 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 + +// 6. 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