From d36946948090fb9e756e046e08a1fa4a9f193b90 Mon Sep 17 00:00:00 2001 From: Parth2009 Date: Sun, 29 Sep 2024 21:52:29 +0530 Subject: [PATCH] excercise level 2 and 3 --- solutions/day-01/countries.js | 13 +++++ solutions/day-01/index.html | 12 ++++ solutions/day-01/level1.js | 31 ++++++++++- solutions/day-01/main.js | 101 ++++++++++++++++++++++++++++++++++ solutions/day-01/web_techs.js | 9 +++ 5 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 solutions/day-01/countries.js create mode 100644 solutions/day-01/index.html create mode 100644 solutions/day-01/main.js create mode 100644 solutions/day-01/web_techs.js diff --git a/solutions/day-01/countries.js b/solutions/day-01/countries.js new file mode 100644 index 0000000..f7fd9ad --- /dev/null +++ b/solutions/day-01/countries.js @@ -0,0 +1,13 @@ +export const countries = [ + "Albania", + "Bolivia", + "Canada", + "Denmark", + "Ethiopia", + "Finland", + "Germany", + "Hungary", + "Ireland", + "Japan", + "Kenya", +]; diff --git a/solutions/day-01/index.html b/solutions/day-01/index.html new file mode 100644 index 0000000..dab0b88 --- /dev/null +++ b/solutions/day-01/index.html @@ -0,0 +1,12 @@ + + + + + + Day 1 + + + + + + \ No newline at end of file diff --git a/solutions/day-01/level1.js b/solutions/day-01/level1.js index 2fde858..cd18513 100644 --- a/solutions/day-01/level1.js +++ b/solutions/day-01/level1.js @@ -1,4 +1,5 @@ // Exercise: Level 1 +console.log("Exercise: Level 1"); const countries = [ "Albania", @@ -83,9 +84,33 @@ console.log( ); // Filter out companies which have more than one 'o' without the filter method -let filteredCompanies = itCompanies.reduce((company) => { - return company.split("o").length - 1 > 0 ? [] : [...company]; -}); +const companies = [ + "Google", + "Microsoft", + "Facebook", + "Amazon", + "Oracle", + "Sony", +]; +const filteredCompanies = []; + +for (let company of companies) { + let oCount = 0; + + // Efficiently count 'o' and break early if more than 1 + for (let char of company) { + if (char.toLowerCase() === "o") { + oCount++; + if (oCount > 1) break; // Stop checking further if more than 1 'o' is found + } + } + + if (oCount <= 1) { + filteredCompanies.push(company); + } +} + +console.log(filteredCompanies); // Sort the array using sort() method itCompanies.sort((a, b) => a.localeCompare(b)); diff --git a/solutions/day-01/main.js b/solutions/day-01/main.js new file mode 100644 index 0000000..5cb2f1c --- /dev/null +++ b/solutions/day-01/main.js @@ -0,0 +1,101 @@ +// Exercise: Level 2 +console.log("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 + +import { countries } from "./countries.js"; +import { webTechs } from "./web_techs.js"; + +// 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."; +let textArray = text.replace(/[^\w\s]|_/g, "").split(" "); +let textCount = textArray.length; +console.log(textArray); +console.log(textCount); +// ["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 +if (!shoppingCart.includes("Meat")) shoppingCart.unshift("Meat"); + +// add Sugar at the end of you shopping cart if it has not been already added +if (!shoppingCart.includes("Sugar")) shoppingCart.push("Sugar"); + +// remove 'Honey' if you are allergic to honey +if (shoppingCart.includes("Honey")) + shoppingCart.splice(shoppingCart.indexOf("Honey"), 1); + +// modify Tea to 'Green Tea' +shoppingCart[shoppingCart.indexOf("Tea")] = "Green Tea"; +console.log(shoppingCart); + +// 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. +if (countries.includes("Ethiopia")) { + console.log("ETHIOPIA"); +} else { + countries.push("Ethiopia"); +} + +// 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. +if (webTechs.includes("Sass")) { + console.log("Sass is a CSS preprocess"); +} else { + webTechs.push("Sass"); + console.log(webTechs); +} + +// Concatenate the following two variables and store it in a fullStack variable. + +const frontEnd = ["HTML", "CSS", "JS", "React", "Redux"]; +const backEnd = ["Node", "Express", "MongoDB"]; +const fullStack = [...frontEnd, ...backEnd]; + +console.log(fullStack); +// ["HTML", "CSS", "JS", "React", "Redux", "Node", "Express", "MongoDB"] + +// Exercise: Level 3 +// The following is an array of 10 students ages: js +const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24]; +// - Sort the array and find the min and max age +ages.sort((a, b) => a - b); +console.log(ages); +// - Find the median age(one middle item or two middle items divided by two) +const middleIndex = Math.floor(ages.length / 2); +const medianAge = + ages.length % 2 !== 0 + ? ages[middleIndex] + : (ages[middleIndex - 1] + ages[middleIndex]) / 2; + +console.log(medianAge); +// - Find the average age(all items divided by number of items) +const sumOfAges = ages.reduce((acc, age) => acc + age, 0); +const averageAge = sumOfAges / ages.length; +console.log(averageAge); + +// - Find the range of the ages(max minus min) +const rangeOfAges = Math.max(...ages) - Math.min(...ages); +console.log(rangeOfAges); + +// - Compare the value of (min - average) and (max - average), use abs() method +const minAvgDiff = Math.abs(Math.min(...ages) - averageAge / ages.length); +const maxAvgDiff = Math.abs(Math.max(...ages) - averageAge / ages.length); +console.log(minAvgDiff); +console.log(maxAvgDiff); + +// 1.Slice the first ten countries from the countries array +console.log(countries.slice(0, 10)); + +// Find the middle country(ies) in the countries array +const middleIndexC = Math.floor(countries.length / 2); +console.log(middleIndexC); + +// Divide the countries array into two equal arrays if it is even. If countries array is not even , one more country for the first half. +const firstHalf = countries.slice(0, middleIndexC + (countries.length % 2)); +const secondHalf = countries.slice(middleIndexC + (countries.length % 2)); +console.log(firstHalf); +console.log(secondHalf); diff --git a/solutions/day-01/web_techs.js b/solutions/day-01/web_techs.js new file mode 100644 index 0000000..63d7ff5 --- /dev/null +++ b/solutions/day-01/web_techs.js @@ -0,0 +1,9 @@ +export const webTechs = [ + "HTML", + "CSS", + "JavaScript", + "React", + "Redux", + "Node", + "MongoDB", +];