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 new file mode 100644 index 0000000..cd18513 --- /dev/null +++ b/solutions/day-01/level1.js @@ -0,0 +1,143 @@ +// Exercise: Level 1 +console.log("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; +let emptyArray = []; + +// Declare an array with more than 5 number of elements +let arrayWithMoreThanFiveElements = [1, 2, 3, 4, 5, 6]; + +// Find the length of your array +let arrayLength = arrayWithMoreThanFiveElements.length; + +// Get the first item, the middle item and the last item of the array +let firstItem = arrayWithMoreThanFiveElements[0]; +let middleItem = arrayWithMoreThanFiveElements[Math.floor(arrayLength / 2)]; +let lastItem = arrayWithMoreThanFiveElements[arrayLength - 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 +let mixedDataTypes = [1, "a", true, 3.14, null, [1]]; + +// Declare an array variable name itCompanies and assign initial values Facebook, Google, Microsoft, Apple, IBM, Oracle and Amazon +let 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]); +console.log(itCompanies[Math.floor(itCompanies.length / 2)]); +console.log(itCompanies[itCompanies.length - 1]); + +// Print out each company +console.log(itCompanies.join(", ")); + +// Change each company name to uppercase one by one and print them out +console.log(itCompanies.map((company) => 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(", ") + .concat(" and ") + .concat(itCompanies[itCompanies.length - 1]) +); + +// Check if a certain company exists in the itCompanies array. If it exist return the company else return a company is not found +console.log( + itCompanies.includes("Google") ? "Company found" : "Company not found" +); + +// Filter out companies which have more than one 'o' without the filter method +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)); + +// Reverse the array using reverse() method +itCompanies.reverse(); + +// Slice out the first 3 companies from the array +itCompanies.slice(0, 3); + +// Slice out the last 3 companies from the array +itCompanies.slice(-3); + +// Slice out the middle IT company or companies from the array +itCompanies.slice( + Math.floor(itCompanies.length / 2), + Math.floor(itCompanies.length / 2) + 2 +); + +// Remove the first IT company from the array +itCompanies.shift(); + +// Remove the middle IT company or companies from the array +itCompanies.splice(Math.floor(itCompanies.length / 2), 1); + +// Remove the last IT company from the array +itCompanies.pop(); + +// Remove all IT companies +itCompanies.splice(0, itCompanies.length); 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", +];