From 56cc092ac9276aac3374529f44fab997d50a34f0 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 21 Nov 2024 16:59:57 +0300 Subject: [PATCH 01/35] test folder created --- Test/1.jsx | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Test/1.jsx diff --git a/Test/1.jsx b/Test/1.jsx new file mode 100644 index 0000000..e69de29 From 9d53e5695a087c8ac35a412a9939c232e11d494f Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 21 Nov 2024 20:46:53 +0300 Subject: [PATCH 02/35] exercice one - array --- Test/01-js-refresher.js | 54 +++++++++++++++++++++++++++++++++++++++++ Test/1.jsx | 0 2 files changed, 54 insertions(+) create mode 100644 Test/01-js-refresher.js delete mode 100644 Test/1.jsx diff --git a/Test/01-js-refresher.js b/Test/01-js-refresher.js new file mode 100644 index 0000000..2589c20 --- /dev/null +++ b/Test/01-js-refresher.js @@ -0,0 +1,54 @@ +// Exercise: Level 1 + +const countries = [ + 'Albania', + 'Bolivia', + 'Canada', + 'Denmark', + 'Ethiopia', + 'Finland', + 'Germany', + 'Hungary', + 'Ireland', + 'Japan', + 'Kenya', +] + +const webTechs = [ + 'HTML', + 'CSS', + 'JavaScript', + 'React', + 'Redux', + 'Node', + 'MongoDB', +] +// Declare an empty array; +const emptyArray = [] +// Declare an array with more than 5 number of elements +const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] +// Find the length of your array +console.log(numbers.length) +// Get the first item, the middle item and the last item of the array +console.log(numbers[0], numbers[Math.floor(numbers.length / 2)], numbers[numbers.length - 1]) +// Declare an array called mixedDataTypes, put different data types in the array and find the length of the array. The array size should be greater than 5 +const mixedDataTypes = [1, 'two', true, { name: 'John' }, [1, 2, 3, 4, 5]] +// Declare an array variable name itCompanies and assign initial values Facebook, Google, Microsoft, Apple, IBM, Oracle and Amazon +const itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'] +// Print the array using console.log() +// Print the number of companies in the array +// Print the first company, middle and last company +// Print out each company +// Change each company name to uppercase one by one and print them out +// Print the array like as a sentence: Facebook, Google, Microsoft, Apple, IBM,Oracle and Amazon are big IT companies. +// Check if a certain company exists in the itCompanies array. If it exist return the company else return a company is not found +// Filter out companies which have more than one 'o' without the filter method +// Sort the array using sort() method +// Reverse the array using reverse() method +// Slice out the first 3 companies from the array +// Slice out the last 3 companies from the array +// Slice out the middle IT company or companies from the array +// Remove the first IT company from the array +// Remove the middle IT company or companies from the array +// Remove the last IT company from the array +// Remove all IT companies \ No newline at end of file diff --git a/Test/1.jsx b/Test/1.jsx deleted file mode 100644 index e69de29..0000000 From 81b694cdc02c73476661a5e66b5d53aab1fb1ab2 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 21 Nov 2024 20:49:09 +0300 Subject: [PATCH 03/35] 50% solved --- Test/01-js-refresher.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Test/01-js-refresher.js b/Test/01-js-refresher.js index 2589c20..2771c0f 100644 --- a/Test/01-js-refresher.js +++ b/Test/01-js-refresher.js @@ -36,10 +36,15 @@ const mixedDataTypes = [1, 'two', true, { name: 'John' }, [1, 2, 3, 4, 5]] // Declare an array variable name itCompanies and assign initial values Facebook, Google, Microsoft, Apple, IBM, Oracle and Amazon const itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'] // Print the array using console.log() +console.log(itCompanies) // Print the number of companies in the array +console.log(itCompanies.length) // Print the first company, middle and last company +console.log(itCompanies[0], itCompanies[Math.floor(itCompanies.length / 2)], itCompanies[itCompanies.length - 1]) // Print out each company +itCompanies.forEach(company => console.log(company)) // Change each company name to uppercase one by one and print them out +itCompanies.forEach(company => console.log(company.toUpperCase())) // Print the array like as a sentence: Facebook, Google, Microsoft, Apple, IBM,Oracle and Amazon are big IT companies. // Check if a certain company exists in the itCompanies array. If it exist return the company else return a company is not found // Filter out companies which have more than one 'o' without the filter method From 2dc864d89be86856b9437edeeb83387aa6804edf Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 21 Nov 2024 20:50:15 +0300 Subject: [PATCH 04/35] 70% solved --- Test/01-js-refresher.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Test/01-js-refresher.js b/Test/01-js-refresher.js index 2771c0f..efc5f64 100644 --- a/Test/01-js-refresher.js +++ b/Test/01-js-refresher.js @@ -46,7 +46,14 @@ itCompanies.forEach(company => console.log(company)) // Change each company name to uppercase one by one and print them out itCompanies.forEach(company => console.log(company.toUpperCase())) // Print the array like as a sentence: Facebook, Google, Microsoft, Apple, IBM,Oracle and Amazon are big IT companies. +console.log(itCompanies.join(', ') + ' are big IT companies.') // Check if a certain company exists in the itCompanies array. If it exist return the company else return a company is not found +const company = 'Google' +if (itCompanies.includes(company)) { + console.log(company +'is found in the array.') +} else { + console.log(company +'is not found in the array.') +} // Filter out companies which have more than one 'o' without the filter method // Sort the array using sort() method // Reverse the array using reverse() method From beb04fab83db230df3f14c8614628ed652c62c5a Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 21 Nov 2024 20:51:32 +0300 Subject: [PATCH 05/35] 80% solved --- Test/01-js-refresher.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Test/01-js-refresher.js b/Test/01-js-refresher.js index efc5f64..6f52d67 100644 --- a/Test/01-js-refresher.js +++ b/Test/01-js-refresher.js @@ -55,9 +55,14 @@ if (itCompanies.includes(company)) { console.log(company +'is not found in the array.') } // Filter out companies which have more than one 'o' without the filter method +const filteredCompanies = itCompanies.filter(company => company.toLowerCase().indexOf('o') === -1) +console.log(filteredCompanies) // Sort the array using sort() method +console.log(itCompanies.sort()) // Reverse the array using reverse() method +console.log(itCompanies.reverse()) // Slice out the first 3 companies from the array +console.log(itCompanies.slice(0, 3)) // Slice out the last 3 companies from the array // Slice out the middle IT company or companies from the array // Remove the first IT company from the array From 5deea33a7e0b63d226909dce8f1e2ecf54640d5b Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 21 Nov 2024 20:52:44 +0300 Subject: [PATCH 06/35] 90% solved --- Test/01-js-refresher.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Test/01-js-refresher.js b/Test/01-js-refresher.js index 6f52d67..3038dfc 100644 --- a/Test/01-js-refresher.js +++ b/Test/01-js-refresher.js @@ -64,8 +64,13 @@ console.log(itCompanies.reverse()) // Slice out the first 3 companies from the array console.log(itCompanies.slice(0, 3)) // Slice out the last 3 companies from the array +console.log(itCompanies.slice(-3)) // Slice out the middle IT company or companies from the array +console.log(itCompanies.slice(3, 4)) // Remove the first IT company from the array +console.log(itCompanies.shift()) // Remove the middle IT company or companies from the array +console.log(itCompanies.splice(3, 1)) // Remove the last IT company from the array +console.log(itCompanies.pop()) // Remove all IT companies \ No newline at end of file From b8eb62789328ee5a4bdf88d93eacca14cb835319 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 21 Nov 2024 20:53:09 +0300 Subject: [PATCH 07/35] 100% solved --- Test/01-js-refresher.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Test/01-js-refresher.js b/Test/01-js-refresher.js index 3038dfc..f796310 100644 --- a/Test/01-js-refresher.js +++ b/Test/01-js-refresher.js @@ -73,4 +73,5 @@ console.log(itCompanies.shift()) console.log(itCompanies.splice(3, 1)) // Remove the last IT company from the array console.log(itCompanies.pop()) -// Remove all IT companies \ No newline at end of file +// Remove all IT companies +console.log(itCompanies.splice(0)) \ No newline at end of file From 5737857e25a4d5a003099927ec794492483b8373 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 21 Nov 2024 20:54:38 +0300 Subject: [PATCH 08/35] folder changed --- Test/{01-js-refresher.js => 1/array-exersise-1.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Test/{01-js-refresher.js => 1/array-exersise-1.js} (100%) diff --git a/Test/01-js-refresher.js b/Test/1/array-exersise-1.js similarity index 100% rename from Test/01-js-refresher.js rename to Test/1/array-exersise-1.js From 26123728ecd26d97f8f0ba1ef9827f24aa112e45 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 21 Nov 2024 20:58:05 +0300 Subject: [PATCH 09/35] exersice two added --- Test/1/array-exersise-2.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Test/1/array-exersise-2.js diff --git a/Test/1/array-exersise-2.js b/Test/1/array-exersise-2.js new file mode 100644 index 0000000..9c44dae --- /dev/null +++ b/Test/1/array-exersise-2.js @@ -0,0 +1,31 @@ +// Exercise: Level 2 +// Create a separate countries.js file and store the countries array into this file, create a separate file web_techs.js and store the webTechs array into this file. Access both file in main.js file + +// First remove all the punctuations and change the string to array and count the number of words in the array + +let text = + 'I love teaching and empowering people. I teach HTML, CSS, JS, React, Python.' +console.log(words) +console.log(words.length) +["I", "love", "teaching", "and", "empowering", "people", "I", "teach", "HTML", "CSS", "JS", "React", "Python"] + +13 +// In the following shopping cart add, remove, edit items + +const shoppingCart = ['Milk', 'Coffee', 'Tea', 'Honey'] + // add 'Meat' in the beginning of your shopping cart if it has not been already added + // add Sugar at the end of you shopping cart if it has not been already added + // remove 'Honey' if you are allergic to honey + // modify Tea to 'Green Tea' + +// In countries array check if 'Ethiopia' exists in the array if it exists print 'ETHIOPIA'. If it does not exist add to the countries list. + +// In the webTechs array check if Sass exists in the array and if it exists print 'Sass is a CSS preprocess'. If it does not exist add Sass to the array and print the array. + +// Concatenate the following two variables and store it in a fullStack variable. + +const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux'] +const backEnd = ['Node', 'Express', 'MongoDB'] + +console.log(fullStack) +["HTML", "CSS", "JS", "React", "Redux", "Node", "Express", "MongoDB"] \ No newline at end of file From 9028b4c5a400749ff268cb63c7fb45bc15491fab Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 21 Nov 2024 21:13:34 +0300 Subject: [PATCH 10/35] object-exercise-1 added and 100% solved --- Test/1/object-exercise-1.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Test/1/object-exercise-1.js diff --git a/Test/1/object-exercise-1.js b/Test/1/object-exercise-1.js new file mode 100644 index 0000000..80e81fd --- /dev/null +++ b/Test/1/object-exercise-1.js @@ -0,0 +1,24 @@ +// Exercises: Level 1 + +// Create an empty object called dog +const dog = {} +// Print the the dog object on the console +console.log(dog) +// Add name, legs, color, age and bark properties for the dog object. The bark property is a method which return woof woof +dog.name = 'Buddy' +dog.legs = 4 +dog.color = 'Golden' +dog.age = 3 +// Get name, legs, color, age and bark value from the dog object +console.log(dog.name) // Output: Buddy +console.log(dog.legs) // Output: 4 +console.log(dog.color) // Output: Golden +console.log(dog.age) // Output: 3 +console.log(dog.bark()) // Output: Woof woof +// Set new properties the dog object: breed, getDogInfo +dog.breed = 'Labrador' +dog.getDogInfo = function() { + return `Name: ${this.name}, Age: ${this.age}, Color: ${this.color}, Breed: ${this.breed}` +} +// Get the dogInfo from the dog object +console.log(dog.getDogInfo()) // Output: Name: Buddy, Age: 3, Color: Golden, Breed: Labrador \ No newline at end of file From 2c65f6240235d945ebaa92189b0d2778e85d9f7b Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 21 Nov 2024 21:27:54 +0300 Subject: [PATCH 11/35] object-exercise-2 10% complated and folder update --- .../1 => Exercises/day-1}/array-exersise-1.js | 0 .../1 => Exercises/day-1}/array-exersise-2.js | 0 .../day-1}/object-exercise-1.js | 0 Exercises/day-1/object-exercise-2.js | 74 +++++++++++++++++++ 4 files changed, 74 insertions(+) rename {Test/1 => Exercises/day-1}/array-exersise-1.js (100%) rename {Test/1 => Exercises/day-1}/array-exersise-2.js (100%) rename {Test/1 => Exercises/day-1}/object-exercise-1.js (100%) create mode 100644 Exercises/day-1/object-exercise-2.js diff --git a/Test/1/array-exersise-1.js b/Exercises/day-1/array-exersise-1.js similarity index 100% rename from Test/1/array-exersise-1.js rename to Exercises/day-1/array-exersise-1.js diff --git a/Test/1/array-exersise-2.js b/Exercises/day-1/array-exersise-2.js similarity index 100% rename from Test/1/array-exersise-2.js rename to Exercises/day-1/array-exersise-2.js diff --git a/Test/1/object-exercise-1.js b/Exercises/day-1/object-exercise-1.js similarity index 100% rename from Test/1/object-exercise-1.js rename to Exercises/day-1/object-exercise-1.js diff --git a/Exercises/day-1/object-exercise-2.js b/Exercises/day-1/object-exercise-2.js new file mode 100644 index 0000000..cd3beff --- /dev/null +++ b/Exercises/day-1/object-exercise-2.js @@ -0,0 +1,74 @@ +const users = { + Alex: { + email: 'alex@alex.com', + skills: ['HTML', 'CSS', 'JavaScript'], + age: 20, + isLoggedIn: false, + points: 30 + }, + Asab: { + email: 'asab@asab.com', + skills: ['HTML', 'CSS', 'JavaScript', 'Redux', 'MongoDB', 'Express', 'React', 'Node'], + age: 25, + isLoggedIn: false, + points: 50 + }, + Brook: { + email: 'daniel@daniel.com', + skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux'], + age: 30, + isLoggedIn: true, + points: 50 + }, + Daniel: { + email: 'daniel@alex.com', + skills: ['HTML', 'CSS', 'JavaScript', 'Python'], + age: 20, + isLoggedIn: false, + points: 40 + }, + John: { + email: 'john@john.com', + skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node.js'], + age: 20, + isLoggedIn: true, + points: 50 + }, + Thomas: { + email: 'thomas@thomas.com', + skills: ['HTML', 'CSS', 'JavaScript', 'React'], + age: 20, + isLoggedIn: false, + points: 40 + }, + Paul: { + email: 'paul@paul.com', + skills: ['HTML', 'CSS', 'JavaScript', 'MongoDB', 'Express', 'React', 'Node'], + age: 20, + isLoggedIn: false, + points: 40 + } +} +// Find the person who has many skills in the users object. + let maxskill =0 + let mostskilledperson=null + for (const user in users){ + const skillcount = users[user].skills.length + if (skillcount > maxskill){ + maxskill = skillcount + mostskilledperson = user + } + } + console.log(`The person with the most skills is ${mostskilledperson} with ${maxskill} skills.`) + +// Count logged in users,count users having greater than equal to 50 points from the following object. + +// Find people who are MERN stack developer from the users object + +// Set your name in the users object without modifying the original users object + +// Get all keys or properties of users object + +// Get all the values of users object + +// Use the countries object to print a country name, capital, populations and languages. \ No newline at end of file From 86707f7bcb01fe372b3d7cc941021916cf68eb10 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 21 Nov 2024 21:45:50 +0300 Subject: [PATCH 12/35] 20% complated --- Exercises/day-1/object-exercise-2.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Exercises/day-1/object-exercise-2.js b/Exercises/day-1/object-exercise-2.js index cd3beff..76a301a 100644 --- a/Exercises/day-1/object-exercise-2.js +++ b/Exercises/day-1/object-exercise-2.js @@ -62,6 +62,19 @@ const users = { console.log(`The person with the most skills is ${mostskilledperson} with ${maxskill} skills.`) // Count logged in users,count users having greater than equal to 50 points from the following object. +let looged = 0 +let person = 0 +for (const user in users ){ + if (users[user].points >= 50 ){ + person++ + + } + if (users[user].isLoggedIn){ + looged++ + } +} + +console.log(`Number of logged in users are ${looged}. ${person} users are greater than 50`) // Find people who are MERN stack developer from the users object From 4927cef459671a4cb3af11d86159d5a3ee6d47a7 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 00:20:02 +0300 Subject: [PATCH 13/35] object-exercise-2 100% complated --- Exercises/day-1/object-exercise-2.js | 52 +++++++++++++++++----------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/Exercises/day-1/object-exercise-2.js b/Exercises/day-1/object-exercise-2.js index 76a301a..52fcf90 100644 --- a/Exercises/day-1/object-exercise-2.js +++ b/Exercises/day-1/object-exercise-2.js @@ -50,33 +50,43 @@ const users = { } } // Find the person who has many skills in the users object. - let maxskill =0 - let mostskilledperson=null - for (const user in users){ - const skillcount = users[user].skills.length - if (skillcount > maxskill){ - maxskill = skillcount - mostskilledperson = user - } - } - console.log(`The person with the most skills is ${mostskilledperson} with ${maxskill} skills.`) +// let maxskill =0 +// let mostskilledperson=null +// for (const user in users){ +// const skillcount = users[user].skills.length +// if (skillcount > maxskill){ +// maxskill = skillcount +// mostskilledperson = user +// } +// } +// console.log(`The person with the most skills is ${mostskilledperson} with ${maxskill} skills.`) // Count logged in users,count users having greater than equal to 50 points from the following object. -let looged = 0 -let person = 0 -for (const user in users ){ - if (users[user].points >= 50 ){ - person++ +// let looged = 0 +// let person = 0 +// for (const user in users ){ +// if (users[user].points >= 50 ){ +// person++ - } - if (users[user].isLoggedIn){ - looged++ - } -} +// } +// if (users[user].isLoggedIn){ +// looged++ +// } +// } -console.log(`Number of logged in users are ${looged}. ${person} users are greater than 50`) +// console.log(`Number of logged in users are ${looged}. ${person} users are greater than 50`) // Find people who are MERN stack developer from the users object +const mern = ["MongoDB", "Express", "React", "Node"]; +let dev = 0; + +for (const user in users) { + if (mern.every(skill => users[user].skills.includes(skill))) { + dev++; + } +} + +console.log(`Number of MERN stack developers are ${dev}.`); // Set your name in the users object without modifying the original users object From d9e83d5d266046b264992b2691f2bb68044276ee Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 00:25:56 +0300 Subject: [PATCH 14/35] 40% complated --- Exercises/day-1/object-exercise-2.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Exercises/day-1/object-exercise-2.js b/Exercises/day-1/object-exercise-2.js index 52fcf90..26b2e86 100644 --- a/Exercises/day-1/object-exercise-2.js +++ b/Exercises/day-1/object-exercise-2.js @@ -89,6 +89,14 @@ for (const user in users) { console.log(`Number of MERN stack developers are ${dev}.`); // Set your name in the users object without modifying the original users object +const copyperson = Object.assign({}, users) +copyperson.Fitsum = { + name: "Fitsum", + skills: ["HTML", "CSS", "JavaScript", "React"], + points: 95, + isLoggedIn: true, +}; +console.log(copyperson.Fitsum); // Get all keys or properties of users object From 3993cb5658d051ca27db3f5a854492be60e8de36 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 00:31:35 +0300 Subject: [PATCH 15/35] 50% complated --- Exercises/day-1/object-exercise-2.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Exercises/day-1/object-exercise-2.js b/Exercises/day-1/object-exercise-2.js index 26b2e86..c1311d3 100644 --- a/Exercises/day-1/object-exercise-2.js +++ b/Exercises/day-1/object-exercise-2.js @@ -77,16 +77,16 @@ const users = { // console.log(`Number of logged in users are ${looged}. ${person} users are greater than 50`) // Find people who are MERN stack developer from the users object -const mern = ["MongoDB", "Express", "React", "Node"]; -let dev = 0; +// const mern = ["MongoDB", "Express", "React", "Node"]; +// let dev = 0; -for (const user in users) { - if (mern.every(skill => users[user].skills.includes(skill))) { - dev++; - } -} +// for (const user in users) { +// if (mern.every(skill => users[user].skills.includes(skill))) { +// dev++; +// } +// } -console.log(`Number of MERN stack developers are ${dev}.`); +// console.log(`Number of MERN stack developers are ${dev}.`); // Set your name in the users object without modifying the original users object const copyperson = Object.assign({}, users) @@ -96,9 +96,12 @@ copyperson.Fitsum = { points: 95, isLoggedIn: true, }; -console.log(copyperson.Fitsum); -// Get all keys or properties of users object +// console.log(copyperson.Fitsum); + +const key = Object.keys(copyperson) +console.log(`Key ${key}`) + // Get all the values of users object From e154454a803ed0849332b7625f2e06f54beec347 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 00:32:58 +0300 Subject: [PATCH 16/35] 90% compalted --- Exercises/day-1/object-exercise-2.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Exercises/day-1/object-exercise-2.js b/Exercises/day-1/object-exercise-2.js index c1311d3..32c69bb 100644 --- a/Exercises/day-1/object-exercise-2.js +++ b/Exercises/day-1/object-exercise-2.js @@ -105,4 +105,7 @@ console.log(`Key ${key}`) // Get all the values of users object +const val = Object.values(copyperson) +console.log(`Value ${val}`) + // Use the countries object to print a country name, capital, populations and languages. \ No newline at end of file From 38653a50b680aeceaa0aa0e0a7af59f40244dbd0 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 00:35:31 +0300 Subject: [PATCH 17/35] 100% complated --- Exercises/day-1/object-exercise-2.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/Exercises/day-1/object-exercise-2.js b/Exercises/day-1/object-exercise-2.js index 32c69bb..e0f51b8 100644 --- a/Exercises/day-1/object-exercise-2.js +++ b/Exercises/day-1/object-exercise-2.js @@ -99,13 +99,28 @@ copyperson.Fitsum = { // console.log(copyperson.Fitsum); -const key = Object.keys(copyperson) -console.log(`Key ${key}`) +// const key = Object.keys(copyperson) +// console.log(`Key ${key}`) // Get all the values of users object -const val = Object.values(copyperson) -console.log(`Value ${val}`) +// const val = Object.values(copyperson) +// console.log(`Value ${val}`) -// Use the countries object to print a country name, capital, populations and languages. \ No newline at end of file +// Use the countries object to print a country name, capital, populations and languages. + +const country = { + name: "Japan", + capital: "Tokyo", + population: 126476461, + languages: ["Japanese"], +} +const getinfo = function (copyperson ) { + console.log(`Country Name: ${copyperson.name}`) + console.log(`Capital: ${copyperson.capital}`) + console.log(`Population: ${copyperson.population}`) + console.log(`Languages: ${copyperson.languages.join(", ")}`) +} + +getinfo(country) \ No newline at end of file From 61a4ba85690669b056e6ffe1a29d86c8708e62b7 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 00:36:48 +0300 Subject: [PATCH 18/35] 100% complated --- Exercises/day-1/object-exercise-2.js | 69 +++++++++++++--------------- 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/Exercises/day-1/object-exercise-2.js b/Exercises/day-1/object-exercise-2.js index e0f51b8..ce2ddf2 100644 --- a/Exercises/day-1/object-exercise-2.js +++ b/Exercises/day-1/object-exercise-2.js @@ -50,43 +50,43 @@ const users = { } } // Find the person who has many skills in the users object. -// let maxskill =0 -// let mostskilledperson=null -// for (const user in users){ -// const skillcount = users[user].skills.length -// if (skillcount > maxskill){ -// maxskill = skillcount -// mostskilledperson = user -// } -// } -// console.log(`The person with the most skills is ${mostskilledperson} with ${maxskill} skills.`) + let maxskill =0 + let mostskilledperson=null + for (const user in users){ + const skillcount = users[user].skills.length + if (skillcount > maxskill){ + maxskill = skillcount + mostskilledperson = user + } + } + console.log(`The person with the most skills is ${mostskilledperson} with ${maxskill} skills.`) // Count logged in users,count users having greater than equal to 50 points from the following object. -// let looged = 0 -// let person = 0 -// for (const user in users ){ -// if (users[user].points >= 50 ){ -// person++ +let looged = 0 +let person = 0 +for (const user in users ){ + if (users[user].points >= 50 ){ + person++ -// } -// if (users[user].isLoggedIn){ -// looged++ -// } -// } + } + if (users[user].isLoggedIn){ + looged++ + } +} -// console.log(`Number of logged in users are ${looged}. ${person} users are greater than 50`) +console.log(`Number of logged in users are ${looged}. ${person} users are greater than 50`) // Find people who are MERN stack developer from the users object -// const mern = ["MongoDB", "Express", "React", "Node"]; -// let dev = 0; +const mern = ["MongoDB", "Express", "React", "Node"]; +let dev = 0; -// for (const user in users) { -// if (mern.every(skill => users[user].skills.includes(skill))) { -// dev++; -// } -// } +for (const user in users) { + if (mern.every(skill => users[user].skills.includes(skill))) { + dev++; + } +} -// console.log(`Number of MERN stack developers are ${dev}.`); +console.log(`Number of MERN stack developers are ${dev}.`); // Set your name in the users object without modifying the original users object const copyperson = Object.assign({}, users) @@ -98,18 +98,15 @@ copyperson.Fitsum = { }; // console.log(copyperson.Fitsum); - -// const key = Object.keys(copyperson) -// console.log(`Key ${key}`) +const key = Object.keys(copyperson) +console.log(`Key ${key}`) // Get all the values of users object - -// const val = Object.values(copyperson) -// console.log(`Value ${val}`) +const val = Object.values(copyperson) +console.log(`Value ${val}`) // Use the countries object to print a country name, capital, populations and languages. - const country = { name: "Japan", capital: "Tokyo", From 0551d2c711e4c28c83aad55a014efbcc5925f15b Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 00:43:02 +0300 Subject: [PATCH 19/35] 10% complated --- Exercises/day-1/object-exercise-3.js | 111 +++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Exercises/day-1/object-exercise-3.js diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js new file mode 100644 index 0000000..28b55bc --- /dev/null +++ b/Exercises/day-1/object-exercise-3.js @@ -0,0 +1,111 @@ +// 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 }); + }, +} + +// **** Questions:2, 3 and 4 are based on the following two arrays:users and products () + +const users = [ + { + _id: 'ab12ex', + username: 'Alex', + email: 'alex@alex.com', + password: '123123', + createdAt: '08/01/2020 9:00 AM', + isLoggedIn: false, + }, + { + _id: 'fg12cy', + username: 'Asab', + email: 'asab@asab.com', + password: '123456', + createdAt: '08/01/2020 9:30 AM', + isLoggedIn: true, + }, + { + _id: 'zwf8md', + username: 'Brook', + email: 'brook@brook.com', + password: '123111', + createdAt: '08/01/2020 9:45 AM', + isLoggedIn: true, + }, + { + _id: 'eefamr', + username: 'Martha', + email: 'martha@martha.com', + password: '123222', + createdAt: '08/01/2020 9:50 AM', + isLoggedIn: false, + }, + { + _id: 'ghderc', + username: 'Thomas', + email: 'thomas@thomas.com', + password: '123333', + createdAt: '08/01/2020 10:00 AM', + isLoggedIn: false, + }, +] + +const products = [ + { + _id: 'eedfcf', + name: 'mobile phone', + description: 'Huawei Honor', + price: 200, + ratings: [ + { userId: 'fg12cy', rate: 5 }, + { userId: 'zwf8md', rate: 4.5 }, + ], + likes: [], + }, + { + _id: 'aegfal', + name: 'Laptop', + description: 'MacPro: System Darwin', + price: 2500, + ratings: [], + likes: ['fg12cy'], + }, + { + _id: 'hedfcg', + name: 'TV', + description: 'Smart TV:Procaster', + price: 400, + ratings: [{ userId: 'fg12cy', rate: 5 }], + likes: ['fg12cy'], + }, +] +// 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 + +// The products array has three elements and each of them has six properties. + // a. Create a function called rateProduct which rates the product + // b. Create a function called averageRating which calculate the average rating of a product + +// 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. \ No newline at end of file From 4982ad8b25ff43f6487d1e341eec1c6dc4566b00 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 00:57:44 +0300 Subject: [PATCH 20/35] 40% complated --- Exercises/day-1/object-exercise-3.js | 71 ++++++++++++++++++---------- 1 file changed, 46 insertions(+), 25 deletions(-) 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 From 6fd93adf685ef9bf1f63517b417e8b9acccba441 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 01:08:56 +0300 Subject: [PATCH 21/35] second option added --- Exercises/day-1/object-exercise-3.js | 33 +++++++++++++++++++--------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index 515414d..c66c54b 100644 --- a/Exercises/day-1/object-exercise-3.js +++ b/Exercises/day-1/object-exercise-3.js @@ -103,7 +103,7 @@ 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. const newuser = { - _id: 'abc123', + _id: 'eedfcf', username: 'John Doe', email: 'johndoe@johndoe.com', password: '123456', @@ -111,18 +111,31 @@ const products = [ 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!') + // const userExists = users.some((user) => user._id === newuser._id); + // if (userExists) { + // console.log('User already exists!') + // return; + // } + // else{ + // users.push(newuser) + // console.log('User added successfully!') + // } + + 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); - + + // 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. From 21e73f5f85e1e81d6623cd9cd851be31b9a7f985 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 10:54:48 +0300 Subject: [PATCH 22/35] 30% compalted --- Exercises/day-1/object-exercise-3.js | 62 +++++++++++++++++----------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index c66c54b..32ad81e 100644 --- a/Exercises/day-1/object-exercise-3.js +++ b/Exercises/day-1/object-exercise-3.js @@ -108,35 +108,51 @@ const products = [ email: 'johndoe@johndoe.com', password: '123456', createdAt: '08/01/2020 10:15 AM', - isLoggedIn: false, + isLoggedIn: null, }; - const signUp = (newuser) => { - // const userExists = users.some((user) => user._id === newuser._id); - // if (userExists) { - // console.log('User already exists!') - // return; - // } - // else{ - // users.push(newuser) - // console.log('User added successfully!') - // } - for (const user of users ){ - if(user._id === newuser._id){ - console.log('User already exists!') - return; +// const signUp = (newuser) => { + +// //solution1 +// const userExists = users.some((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; +// } +// } +// } +// signUp(newuser); + + + // b. Create a function called signIn which allows user to sign in to the application + const signIn = (newuser) => { + const exists = users.find(user => user._id === newuser._id); + if (exists){ + exists.isLoggedIn = true; + console.log ("user signIn success") } else{ - users.push(newuser) - console.log('User added successfully!') - break; + exists.isLoggedIn = false; + console.log ("account does not exist") } + } - } - 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 From 7eb4cd58d551f31f7a08b32f8a159d4a4b57eb7f Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 20:53:38 +0300 Subject: [PATCH 23/35] 30% compalted --- Exercises/day-1/object-exercise-3.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index 32ad81e..c2a2824 100644 --- a/Exercises/day-1/object-exercise-3.js +++ b/Exercises/day-1/object-exercise-3.js @@ -3,7 +3,7 @@ // 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', +// lastName: 'helina' // incomes: [ // { description: 'Salary', amount: 50000 }, // { description: 'Bonus', amount: 10000 }, @@ -155,6 +155,7 @@ const products = [ } // The products array has three elements and each of them has six properties. + // a. Create a function called rateProduct which rates the product // b. Create a function called averageRating which calculate the average rating of a product From 721ad7cf91b587fc9c0a7e72550e399321d3c3b5 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 21:28:13 +0300 Subject: [PATCH 24/35] product rating 100% complated --- Exercises/day-1/object-exercise-3.js | 46 +++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index c2a2824..24797ee 100644 --- a/Exercises/day-1/object-exercise-3.js +++ b/Exercises/day-1/object-exercise-3.js @@ -155,8 +155,52 @@ const products = [ } // The products array has three elements and each of them has six properties. - + const product =[ + { + _id: 'eedfcf', + name: 'Mobile phone', + description: 'Huawei Honor', + price: 200, + ratings: [ + { userId: 'fg12cy', rate: 5 }, + { userId: 'zwf8md', rate: 4.5 }, + ], + likes: [], + }, + { + _id: 'aegfal', + name: 'Laptop', + description: 'MacPro: System Darwin', + price: 2500, + ratings: [], + likes: ['fg12cy'], + }, + { + _id: 'hedfcg', + name: 'TV', + description: 'Smart TV: Procaster', + price: 400, + ratings: [{ userId: 'fg12cy', rate: 5 }], + likes: ['fg12cy'], + }, + ] + // a. Create a function called rateProduct which rates the product + const rateproduct = (productId , userid , rate ) => { + const product = product.find (item=>productId===item._id) + if (product){ + const exixst = product.ratings.find(rating => userid===rating.userId) + if(exixst){ + console.log("user already rated this product") + } + else{ + product.rating.push({userid ,rate}) + } + } + else{ + console.log("product not found") + } + } // b. Create a function called averageRating which calculate the average rating of a product // 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. \ No newline at end of file From ecbb5332a0e44486ba42aaa60b2bc68134c4d6e3 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 21:31:54 +0300 Subject: [PATCH 25/35] product rating 100% complated --- Exercises/day-1/object-exercise-3.js | 215 +++++++++++++-------------- 1 file changed, 105 insertions(+), 110 deletions(-) diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index 24797ee..134c7d7 100644 --- a/Exercises/day-1/object-exercise-3.js +++ b/Exercises/day-1/object-exercise-3.js @@ -30,86 +30,86 @@ const users = [ { - _id: 'ab12ex', - username: 'Alex', - email: 'alex@alex.com', - password: '123123', - createdAt: '08/01/2020 9:00 AM', + _id: "ab12ex", + username: "Alex", + email: "alex@alex.com", + password: "123123", + createdAt: "08/01/2020 9:00 AM", isLoggedIn: false, }, { - _id: 'fg12cy', - username: 'Asab', - email: 'asab@asab.com', - password: '123456', - createdAt: '08/01/2020 9:30 AM', + _id: "fg12cy", + username: "Asab", + email: "asab@asab.com", + password: "123456", + createdAt: "08/01/2020 9:30 AM", isLoggedIn: true, }, { - _id: 'zwf8md', - username: 'Brook', - email: 'brook@brook.com', - password: '123111', - createdAt: '08/01/2020 9:45 AM', + _id: "zwf8md", + username: "Brook", + email: "brook@brook.com", + password: "123111", + createdAt: "08/01/2020 9:45 AM", isLoggedIn: true, }, { - _id: 'eefamr', - username: 'Martha', - email: 'martha@martha.com', - password: '123222', - createdAt: '08/01/2020 9:50 AM', + _id: "eefamr", + username: "Martha", + email: "martha@martha.com", + password: "123222", + createdAt: "08/01/2020 9:50 AM", isLoggedIn: false, }, { - _id: 'ghderc', - username: 'Thomas', - email: 'thomas@thomas.com', - password: '123333', - createdAt: '08/01/2020 10:00 AM', + _id: "ghderc", + username: "Thomas", + email: "thomas@thomas.com", + password: "123333", + createdAt: "08/01/2020 10:00 AM", isLoggedIn: false, }, -] +]; const products = [ { - _id: 'eedfcf', - name: 'mobile phone', - description: 'Huawei Honor', + _id: "eedfcf", + name: "mobile phone", + description: "Huawei Honor", price: 200, ratings: [ - { userId: 'fg12cy', rate: 5 }, - { userId: 'zwf8md', rate: 4.5 }, + { userId: "fg12cy", rate: 5 }, + { userId: "zwf8md", rate: 4.5 }, ], likes: [], }, { - _id: 'aegfal', - name: 'Laptop', - description: 'MacPro: System Darwin', + _id: "aegfal", + name: "Laptop", + description: "MacPro: System Darwin", price: 2500, ratings: [], - likes: ['fg12cy'], + likes: ["fg12cy"], }, { - _id: 'hedfcg', - name: 'TV', - description: 'Smart TV:Procaster', + _id: "hedfcg", + name: "TV", + description: "Smart TV:Procaster", price: 400, - ratings: [{ userId: 'fg12cy', rate: 5 }], - likes: ['fg12cy'], + ratings: [{ userId: "fg12cy", rate: 5 }], + likes: ["fg12cy"], }, -] +]; // 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. - const newuser = { - _id: 'eedfcf', - username: 'John Doe', - email: 'johndoe@johndoe.com', - password: '123456', - createdAt: '08/01/2020 10:15 AM', - isLoggedIn: null, - }; +// 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: "eedfcf", + username: "John Doe", + email: "johndoe@johndoe.com", + password: "123456", + createdAt: "08/01/2020 10:15 AM", + isLoggedIn: null, +}; // const signUp = (newuser) => { @@ -138,69 +138,64 @@ const products = [ // } // } // signUp(newuser); - - // b. Create a function called signIn which allows user to sign in to the application - const signIn = (newuser) => { - const exists = users.find(user => user._id === newuser._id); - if (exists){ - exists.isLoggedIn = true; - console.log ("user signIn success") - } - else{ - exists.isLoggedIn = false; - console.log ("account does not exist") - } - - } +// b. Create a function called signIn which allows user to sign in to the application +const signIn = (newuser) => { + const exists = users.find((user) => user._id === newuser._id); + if (exists) { + exists.isLoggedIn = true; + console.log("user signIn success"); + } else { + exists.isLoggedIn = false; + console.log("account does not exist"); + } +}; // The products array has three elements and each of them has six properties. - const product =[ - { - _id: 'eedfcf', - name: 'Mobile phone', - description: 'Huawei Honor', - price: 200, - ratings: [ - { userId: 'fg12cy', rate: 5 }, - { userId: 'zwf8md', rate: 4.5 }, - ], - likes: [], - }, - { - _id: 'aegfal', - name: 'Laptop', - description: 'MacPro: System Darwin', - price: 2500, - ratings: [], - likes: ['fg12cy'], - }, - { - _id: 'hedfcg', - name: 'TV', - description: 'Smart TV: Procaster', - price: 400, - ratings: [{ userId: 'fg12cy', rate: 5 }], - likes: ['fg12cy'], - }, - ] +const product = [ + { + _id: "eedfcf", + name: "Mobile phone", + description: "Huawei Honor", + price: 200, + ratings: [ + { userId: "fg12cy", rate: 5 }, + { userId: "zwf8md", rate: 4.5 }, + ], + likes: [], + }, + { + _id: "aegfal", + name: "Laptop", + description: "MacPro: System Darwin", + price: 2500, + ratings: [], + likes: ["fg12cy"], + }, + { + _id: "hedfcg", + name: "TV", + description: "Smart TV: Procaster", + price: 400, + ratings: [{ userId: "fg12cy", rate: 5 }], + likes: ["fg12cy"], + }, +]; - // a. Create a function called rateProduct which rates the product - const rateproduct = (productId , userid , rate ) => { - const product = product.find (item=>productId===item._id) - if (product){ - const exixst = product.ratings.find(rating => userid===rating.userId) - if(exixst){ - console.log("user already rated this product") - } - else{ - product.rating.push({userid ,rate}) - } - } - else{ - console.log("product not found") - } +// a. Create a function called rateProduct which rates the product +const rateproduct = (productId, userid, rate) => { + const product = product.find((item) => productId === item._id); + if (product) { + const exixst = product.ratings.find((rating) => userid === rating.userId); + if (exixst) { + console.log("user already rated this product"); + } else { + product.rating.push({ userid, rate }); } - // b. Create a function called averageRating which calculate the average rating of a product + } else { + console.log("product not found"); + } +}; +// b. Create a function called averageRating which calculate the average rating of a product -// 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. \ No newline at end of file +// 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. From 11e5194dfc2f3c8d0eddb8e482a5e4aa8bd18981 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 22:47:03 +0300 Subject: [PATCH 26/35] likeproduct 100%compalted --- Exercises/day-1/object-exercise-3.js | 128 +++++++++++++++++---------- 1 file changed, 79 insertions(+), 49 deletions(-) diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index 134c7d7..f080f07 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 () @@ -111,33 +111,32 @@ const newuser = { isLoggedIn: null, }; -// const signUp = (newuser) => { - + const signUp = (newuser) => { // //solution1 -// const userExists = users.some((user) => user._id === newuser._id); -// if (userExists) { -// console.log('User already exists!') -// return; -// } -// else{ -// users.push(newuser) -// console.log('User added successfully!') -// } + 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; -// } -// } -// } -// signUp(newuser); +// 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); // b. Create a function called signIn which allows user to sign in to the application const signIn = (newuser) => { @@ -197,5 +196,36 @@ 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"); + } +} // 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}`); + } + } else { + console.log("product not found"); + } +} \ No newline at end of file From 656beaa7b87bda5085a4fcd8ec37924028d41eec Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 23:26:32 +0300 Subject: [PATCH 27/35] function exresice 1 90% complated --- Exercises/day-1/function-exersice-1.js | 30 +++++ Exercises/day-1/object-exercise-3.js | 145 ++++++++++++------------- 2 files changed, 102 insertions(+), 73 deletions(-) create mode 100644 Exercises/day-1/function-exersice-1.js 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"); + } +}; From d9cc6e0036f07f80bdba7f6e23f6f2e88ef5bc7c Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Fri, 22 Nov 2024 23:31:44 +0300 Subject: [PATCH 28/35] 100% compalted --- Exercises/day-1/function-exersice-1.js | 30 ++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/Exercises/day-1/function-exersice-1.js b/Exercises/day-1/function-exersice-1.js index 55676ae..16d0b14 100644 --- a/Exercises/day-1/function-exersice-1.js +++ b/Exercises/day-1/function-exersice-1.js @@ -20,11 +20,37 @@ const temperature = (oC) => { } // 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. - + const BMI = (W,H) => { + let bmi= W / (H * H) + if (bmi>18.4 || bmi<25){ + return 'Normal weight' + } + else if (bmi>25){ + return 'Overweight' + } + else{ + return 'Underweight' + } + } // 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 +// Write a function called checkSeason, it takes a month parameter and returns the season:Autumn, Winter, Spring or Summer. + const checkSeason = (month) => { + if (month >= 3 && month <= 5){ + return 'Spring' + } + else if (month >= 6 && month <= 8){ + return 'Summer' + } + else if (month >= 9 && month <= 11){ + return 'Autumn' + } + else{ + return 'Winter' + } + } \ No newline at end of file From 350fcf6335e52b74fabba6280738efa1f3874793 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Sat, 23 Nov 2024 21:36:23 +0300 Subject: [PATCH 29/35] replece() method 100% --- Exercises/day-1/array-exersise-2.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Exercises/day-1/array-exersise-2.js b/Exercises/day-1/array-exersise-2.js index 9c44dae..c7a958d 100644 --- a/Exercises/day-1/array-exersise-2.js +++ b/Exercises/day-1/array-exersise-2.js @@ -3,13 +3,8 @@ // First remove all the punctuations and change the string to array and count the number of words in the array -let text = - 'I love teaching and empowering people. I teach HTML, CSS, JS, React, Python.' -console.log(words) -console.log(words.length) -["I", "love", "teaching", "and", "empowering", "people", "I", "teach", "HTML", "CSS", "JS", "React", "Python"] - -13 +let text ='I love teaching and empowering people. I teach HTML, CSS, JS, React, Python.' +const word = text.replace(/[,.]/ ,'') // In the following shopping cart add, remove, edit items const shoppingCart = ['Milk', 'Coffee', 'Tea', 'Honey'] @@ -28,4 +23,4 @@ const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux'] const backEnd = ['Node', 'Express', 'MongoDB'] console.log(fullStack) -["HTML", "CSS", "JS", "React", "Redux", "Node", "Express", "MongoDB"] \ No newline at end of file +["HTML", "CSS", "JS", "React", "Redux", "Node", "Express", "MongoDB"] From 040f1bc4b88a590d66aa558b24519cd3d49b94b4 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Sat, 23 Nov 2024 22:06:33 +0300 Subject: [PATCH 30/35] replece() method 100% --- Exercises/day-1/array-exersise-2.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Exercises/day-1/array-exersise-2.js b/Exercises/day-1/array-exersise-2.js index c7a958d..c39a7b5 100644 --- a/Exercises/day-1/array-exersise-2.js +++ b/Exercises/day-1/array-exersise-2.js @@ -1,10 +1,12 @@ // Exercise: Level 2 // Create a separate countries.js file and store the countries array into this file, create a separate file web_techs.js and store the webTechs array into this file. Access both file in main.js file + // First remove all the punctuations and change the string to array and count the number of words in the array let text ='I love teaching and empowering people. I teach HTML, CSS, JS, React, Python.' -const word = text.replace(/[,.]/ ,'') +const word = text.replace(/[,.]/gi ,'').split(' ') +console.log(word) // In the following shopping cart add, remove, edit items const shoppingCart = ['Milk', 'Coffee', 'Tea', 'Honey'] From f18a4762fc93e053152dd529b8a253459ad772b7 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 28 Nov 2024 13:34:08 +0300 Subject: [PATCH 31/35] some change --- Exercises/day-1/object-exercise-3.js | 2 +- Exercises/test/test.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 Exercises/test/test.js diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index ce68683..2be77c6 100644 --- a/Exercises/day-1/object-exercise-3.js +++ b/Exercises/day-1/object-exercise-3.js @@ -222,7 +222,7 @@ const likeProduct = (productId, userId, products) => { console.log(`${userId} has removed like from ${product.name}`); } else { product.likes.push(userId); - console.log(`${userId} has liked ${product.name}`); + console.log(`${userId} has liked ${product.name}`); } } else { console.log("product not found"); diff --git a/Exercises/test/test.js b/Exercises/test/test.js new file mode 100644 index 0000000..3960126 --- /dev/null +++ b/Exercises/test/test.js @@ -0,0 +1,13 @@ +arr=[1,2,3,4,5] + + +const squre = () => { + const result =[] + arr.forEach(element => { + result.push( element*2 ) + + }); + return result +} + +console.log(squre()) \ No newline at end of file From 9207ca623027da65719e01ad312525baf1b9471f Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 28 Nov 2024 13:49:38 +0300 Subject: [PATCH 32/35] fuction exercise one 100% commplated --- Exercises/day-1/function-exercise-1.js | 34 ++++++++++++++++++++++++++ Exercises/test/test.js | 16 ++++-------- 2 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 Exercises/day-1/function-exercise-1.js diff --git a/Exercises/day-1/function-exercise-1.js b/Exercises/day-1/function-exercise-1.js new file mode 100644 index 0000000..6122c0e --- /dev/null +++ b/Exercises/day-1/function-exercise-1.js @@ -0,0 +1,34 @@ +// Create a function called getPersonInfo. The getPersonInfo function takes an object parameter. +// The structure of the object and the output of the function is given below. Try to use both a regular way and destructuring +// and compare the cleanness of the code. If you want to compare your solution with my solution, check this link. + +const person = { + firstName: 'Asabeneh', + lastName: 'Yetayeh', + age: 250, + country: 'Finland', + job: 'Instructor and Developer', + skills: [ + 'HTML', + 'CSS', + 'JavaScript', + 'React', + 'Redux', + 'Node', + 'MongoDB', + 'Python', + 'D3.js', + ], + languages: ['Amharic', 'English', 'Suomi(Finnish)'], +} + +const getPersonInfo = (person) => { + const {firstName, lastName, age, country, job, skills, languages} = person; + const {skill1,skill2,skill3,skill4,skill5,skill6,skill7,skill8,skill9} = skills; + console.log(`${firstName} ${lastName} lives in ${country}. He is ${age} years old. He is an ${job}. He teaches ${skill1}, ${skill2}, ${skill3}, ${skill4}, ${skill5}, ${skill6}, ${skill7}, ${skill8}, ${skill9}. He speaks ${languages[0]}, ${languages[1]} and a little bit of ${languages[2]}`); +} + +getPersonInfo(person) +/* +Asabeneh Yetayeh lives in Finland. He is 250 years old. He is an Instructor and Developer. He teaches HTML, CSS, JavaScript, React, Redux, Node, MongoDB, Python and D3.js. He speaks Amharic, English and a little bit of Suomi(Finnish) +*/ \ No newline at end of file diff --git a/Exercises/test/test.js b/Exercises/test/test.js index 3960126..ab52d3a 100644 --- a/Exercises/test/test.js +++ b/Exercises/test/test.js @@ -1,13 +1,7 @@ -arr=[1,2,3,4,5] - - -const squre = () => { - const result =[] - arr.forEach(element => { - result.push( element*2 ) - - }); - return result +const rectangle = { + width: 20, + height: 10, } -console.log(squre()) \ No newline at end of file +let { width, height, perimeter = 200 } = rectangle +console.log(width, height, perimeter) \ No newline at end of file From 888772ec4a3587ee2e4284408e7430c616d37644 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 28 Nov 2024 20:26:58 +0300 Subject: [PATCH 33/35] test file addded --- Exercises/test/test.js | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/Exercises/test/test.js b/Exercises/test/test.js index ab52d3a..e313c99 100644 --- a/Exercises/test/test.js +++ b/Exercises/test/test.js @@ -1,7 +1,34 @@ -const rectangle = { - width: 20, - height: 10, -} +// const arr = ['Finland', 'Estonia', 'Sweden', 'Norway'] +// const arr2 = [] +// arr.forEach((country) => arr2.push(country.toUpperCase())) +// console.log(arr2) -let { width, height, perimeter = 200 } = rectangle -console.log(width, height, perimeter) \ No newline at end of file + +// const arr = [1,2,3,4,5] +// let sum = 0 + +// const add = (num,i,arr) =>{ +// sum+=num +// } +// arr.forEach(add) +// console.log(sum) + +// const hey = () => { +// console.log('hey') +// } +// function hello() { +// console.log('hello') +// } +// hello() +// hey() + +// const countries = ['Finland', 'Estonia', 'Sweden', 'Norway'] +// console.log(countries.map((country) => country.toUpperCase())) +// const newarr = [] +// countries.forEach(x => { +// if (x.includes('land')) { +// newarr.push(x) +// } + +// }); +// console.log(newarr) \ No newline at end of file From 0b59f7a8f5b9da499c4c07bfb3bddd02d941c02c Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 28 Nov 2024 22:18:19 +0300 Subject: [PATCH 34/35] some change --- Exercises/day-1/object-exercise-3.js | 353 ++++++++++++++------------- Exercises/test/test.js | 14 +- 2 files changed, 202 insertions(+), 165 deletions(-) diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index 2be77c6..f81ec01 100644 --- a/Exercises/day-1/object-exercise-3.js +++ b/Exercises/day-1/object-exercise-3.js @@ -1,75 +1,75 @@ -// 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 }); - }, -}; +// // Exercises: Level 3 -// **** Questions:2, 3 and 4 are based on the following two arrays:users and products () +// // 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 users = [ - { - _id: "ab12ex", - username: "Alex", - email: "alex@alex.com", - password: "123123", - createdAt: "08/01/2020 9:00 AM", - isLoggedIn: false, - }, - { - _id: "fg12cy", - username: "Asab", - email: "asab@asab.com", - password: "123456", - createdAt: "08/01/2020 9:30 AM", - isLoggedIn: true, - }, - { - _id: "zwf8md", - username: "Brook", - email: "brook@brook.com", - password: "123111", - createdAt: "08/01/2020 9:45 AM", - isLoggedIn: true, - }, - { - _id: "eefamr", - username: "Martha", - email: "martha@martha.com", - password: "123222", - createdAt: "08/01/2020 9:50 AM", - isLoggedIn: false, - }, - { - _id: "ghderc", - username: "Thomas", - email: "thomas@thomas.com", - password: "123333", - createdAt: "08/01/2020 10:00 AM", - isLoggedIn: false, - }, -]; +// // **** Questions:2, 3 and 4 are based on the following two arrays:users and products () + +// const users = [ +// { +// _id: "ab12ex", +// username: "Alex", +// email: "alex@alex.com", +// password: "123123", +// createdAt: "08/01/2020 9:00 AM", +// isLoggedIn: false, +// }, +// { +// _id: "fg12cy", +// username: "Asab", +// email: "asab@asab.com", +// password: "123456", +// createdAt: "08/01/2020 9:30 AM", +// isLoggedIn: true, +// }, +// { +// _id: "zwf8md", +// username: "Brook", +// email: "brook@brook.com", +// password: "123111", +// createdAt: "08/01/2020 9:45 AM", +// isLoggedIn: true, +// }, +// { +// _id: "eefamr", +// username: "Martha", +// email: "martha@martha.com", +// password: "123222", +// createdAt: "08/01/2020 9:50 AM", +// isLoggedIn: false, +// }, +// { +// _id: "ghderc", +// username: "Thomas", +// email: "thomas@thomas.com", +// password: "123333", +// createdAt: "08/01/2020 10:00 AM", +// isLoggedIn: false, +// }, +// ]; const products = [ { @@ -100,53 +100,54 @@ const products = [ likes: ["fg12cy"], }, ]; -// 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. -const newuser = { - _id: "eedfcf", - username: "John Doe", - email: "johndoe@johndoe.com", - password: "123456", - createdAt: "08/01/2020 10:15 AM", - 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!"); - } - - // 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); - -// b. Create a function called signIn which allows user to sign in to the application -const signIn = (newuser) => { - const exists = users.find((user) => user._id === newuser._id); - if (exists) { - exists.isLoggedIn = true; - console.log("user signIn success"); - } else { - exists.isLoggedIn = false; - console.log("account does not exist"); - } -}; +// // 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. +// const newuser = { +// _id: "eedfcf", +// username: "John Doe", +// email: "johndoe@johndoe.com", +// password: "123456", +// createdAt: "08/01/2020 10:15 AM", +// 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!"); +// } + +// // 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); + +// // b. Create a function called signIn which allows user to sign in to the application +// const signIn = (newuser) => { +// const exists = users.find((user) => user._id === newuser._id); +// if (exists) { +// exists.isLoggedIn = true; +// console.log("user signIn success"); +// } else { +// exists.isLoggedIn = false; +// console.log("account does not exist"); +// } +// }; // The products array has three elements and each of them has six properties. const product = [ @@ -174,57 +175,83 @@ const product = [ name: "TV", description: "Smart TV: Procaster", price: 400, - ratings: [{ userId: "fg12cy", rate: 5 }], + ratings: [{ userId: "fg12cy", rate: 5 }, { userId: "zwf8md", rate: 3.5 },], likes: ["fg12cy"], }, ]; // a. Create a function called rateProduct which rates the product -const rateproduct = (productId, userid, rate) => { - const product = product.find((item) => productId === item._id); - if (product) { - const exixst = product.ratings.find((rating) => userid === rating.userId); - if (exixst) { - console.log("user already rated this product"); - } else { - product.rating.push({ userid, rate }); - } - } else { - console.log("product not found"); - } -}; -// 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 rateproduct = (productId, userid, rate) => { +// const product = product.find((item) => productId === item._id); +// if (product) { +// const exixst = product.ratings.find((rating) => userid === rating.userId); +// if (exixst) { +// console.log("user already rated this product"); +// } else { +// product.rating.push({ userid, rate }); +// } +// } 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}`); - } - } else { - console.log("product not found"); - } +// // 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"); +// } +// }; + +// // 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}`); +// } +// } else { +// console.log("product not found"); +// } +// }; + + +// const userratedproduct = (productId, userId, products) => { +// const rated = products.filter((item) => item.rating.userId===userId) +// if (rated != 0) { +// const product = products.filter((item) => productId === item._id); +// if (product) { +// console.log(`${userId} has rated ${product.name}`); +// } else { +// console.log("product not found"); +// } +// } +// } + +const userRatedProducts = (userId, products) => { + + const ratedProducts = products.filter((item) => + item.ratings.find((rating) => rating.userId === userId) + ); + return ratedProducts.map((item) => item.name); }; + +const ratedByUser = userRatedProducts("zwf8md", product); +console.log(ratedByUser); + diff --git a/Exercises/test/test.js b/Exercises/test/test.js index e313c99..157aa29 100644 --- a/Exercises/test/test.js +++ b/Exercises/test/test.js @@ -23,7 +23,6 @@ // hey() // const countries = ['Finland', 'Estonia', 'Sweden', 'Norway'] -// console.log(countries.map((country) => country.toUpperCase())) // const newarr = [] // countries.forEach(x => { // if (x.includes('land')) { @@ -31,4 +30,15 @@ // } // }); -// console.log(newarr) \ No newline at end of file +// console.log(countries.map((country) => country.toUpperCase())) +// console.log(countries.filter((country) => !country.includes('land'))) +// console.log(newarr) + +// const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +// const sum = numbers.reduce((x, y) => x + y) +// console.log(sum) // 55 + +// const numbers = [1, 2, 3, 4, 5] +// const value = numbers.reduce((acc, cur) => acc * cur ,0) +// console.log(value) // 0 + From 4c7d6a657a5de277ab4a01b3a6c257684debac06 Mon Sep 17 00:00:00 2001 From: Fitsumhelina Date: Thu, 28 Nov 2024 22:38:39 +0300 Subject: [PATCH 35/35] user rate product count update --- Exercises/day-1/object-exercise-3.js | 176 ++++++++++++++++----------- 1 file changed, 103 insertions(+), 73 deletions(-) diff --git a/Exercises/day-1/object-exercise-3.js b/Exercises/day-1/object-exercise-3.js index f81ec01..ab9befb 100644 --- a/Exercises/day-1/object-exercise-3.js +++ b/Exercises/day-1/object-exercise-3.js @@ -28,48 +28,48 @@ // // **** Questions:2, 3 and 4 are based on the following two arrays:users and products () -// const users = [ -// { -// _id: "ab12ex", -// username: "Alex", -// email: "alex@alex.com", -// password: "123123", -// createdAt: "08/01/2020 9:00 AM", -// isLoggedIn: false, -// }, -// { -// _id: "fg12cy", -// username: "Asab", -// email: "asab@asab.com", -// password: "123456", -// createdAt: "08/01/2020 9:30 AM", -// isLoggedIn: true, -// }, -// { -// _id: "zwf8md", -// username: "Brook", -// email: "brook@brook.com", -// password: "123111", -// createdAt: "08/01/2020 9:45 AM", -// isLoggedIn: true, -// }, -// { -// _id: "eefamr", -// username: "Martha", -// email: "martha@martha.com", -// password: "123222", -// createdAt: "08/01/2020 9:50 AM", -// isLoggedIn: false, -// }, -// { -// _id: "ghderc", -// username: "Thomas", -// email: "thomas@thomas.com", -// password: "123333", -// createdAt: "08/01/2020 10:00 AM", -// isLoggedIn: false, -// }, -// ]; +const users = [ + { + _id: "ab12ex", + username: "Alex", + email: "alex@alex.com", + password: "123123", + createdAt: "08/01/2020 9:00 AM", + isLoggedIn: false, + }, + { + _id: "fg12cy", + username: "Asab", + email: "asab@asab.com", + password: "123456", + createdAt: "08/01/2020 9:30 AM", + isLoggedIn: true, + }, + { + _id: "zwf8md", + username: "Brook", + email: "brook@brook.com", + password: "123111", + createdAt: "08/01/2020 9:45 AM", + isLoggedIn: true, + }, + { + _id: "eefamr", + username: "Martha", + email: "martha@martha.com", + password: "123222", + createdAt: "08/01/2020 9:50 AM", + isLoggedIn: false, + }, + { + _id: "ghderc", + username: "Thomas", + email: "thomas@thomas.com", + password: "123333", + createdAt: "08/01/2020 10:00 AM", + isLoggedIn: false, + }, +]; const products = [ { @@ -79,7 +79,7 @@ const products = [ price: 200, ratings: [ { userId: "fg12cy", rate: 5 }, - { userId: "zwf8md", rate: 4.5 }, + { userId: "ab12ex", rate: 4.5 }, ], likes: [], }, @@ -150,35 +150,35 @@ const products = [ // }; // The products array has three elements and each of them has six properties. -const product = [ - { - _id: "eedfcf", - name: "Mobile phone", - description: "Huawei Honor", - price: 200, - ratings: [ - { userId: "fg12cy", rate: 5 }, - { userId: "zwf8md", rate: 4.5 }, - ], - likes: [], - }, - { - _id: "aegfal", - name: "Laptop", - description: "MacPro: System Darwin", - price: 2500, - ratings: [], - likes: ["fg12cy"], - }, - { - _id: "hedfcg", - name: "TV", - description: "Smart TV: Procaster", - price: 400, - ratings: [{ userId: "fg12cy", rate: 5 }, { userId: "zwf8md", rate: 3.5 },], - likes: ["fg12cy"], - }, -]; +// const product = [ +// { +// _id: "eedfcf", +// name: "Mobile phone", +// description: "Huawei Honor", +// price: 200, +// ratings: [ +// { userId: "fg12cy", rate: 5 }, +// { userId: "zwf8md", rate: 4.5 }, +// ], +// likes: [], +// }, +// { +// _id: "aegfal", +// name: "Laptop", +// description: "MacPro: System Darwin", +// price: 2500, +// ratings: [], +// likes: ["fg12cy"], +// }, +// { +// _id: "hedfcg", +// name: "TV", +// description: "Smart TV: Procaster", +// price: 400, +// ratings: [{ userId: "aegfal", rate: 5 }, { userId: "zwf8md", rate: 3.5 },], +// likes: ["fg12cy"], +// }, +// ]; // a. Create a function called rateProduct which rates the product // const rateproduct = (productId, userid, rate) => { @@ -195,6 +195,34 @@ const product = [ // } // }; + +// const rateProduct = (productId, userId, rate) => { +// const product = products.find((product) => product._id === productId ); +// if (product) { +// const user = users.find((users) => users._id === userId); +// if (user) { +// const existingRating = product.ratings.find( +// (rating) => rating.userId === userId +// ); +// if(!existingRating){ +// product.ratings.push({ userId: users._id, rate }); +// console.log("Product rating updated successfully");} +// else{ +// console.log("User already rated this product"); +// } +// } +// else{ +// console.log("User not found"); +// } +// } +// else{ +// console.log("Product not found"); +// } +// } +// rateProduct("aegfal", "ab12ex", 2); + + + // // 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); @@ -244,6 +272,7 @@ const product = [ // } // } + const userRatedProducts = (userId, products) => { const ratedProducts = products.filter((item) => @@ -252,6 +281,7 @@ const userRatedProducts = (userId, products) => { return ratedProducts.map((item) => item.name); }; -const ratedByUser = userRatedProducts("zwf8md", product); +const ratedByUser = userRatedProducts("ab12ex", products); console.log(ratedByUser); +