From 2d6f8f825dd2c0b74a3b726b38282da9827b9fc0 Mon Sep 17 00:00:00 2001 From: leon-vay Date: Tue, 8 Aug 2023 21:17:52 -0700 Subject: [PATCH] finished level 2. Started level 3 --- solutions/day-01/countries.js | 15 --------- solutions/day-01/level2.js | 48 +++++++++++++++++++++++++--- solutions/day-01/level3.js | 59 +++++++++++++++++++++++++++++++++++ solutions/day-01/web_tech.js | 11 ------- solutions/index.html | 9 ++++++ 5 files changed, 112 insertions(+), 30 deletions(-) delete mode 100644 solutions/day-01/countries.js create mode 100644 solutions/day-01/level3.js delete mode 100644 solutions/day-01/web_tech.js create mode 100644 solutions/index.html diff --git a/solutions/day-01/countries.js b/solutions/day-01/countries.js deleted file mode 100644 index 2b703a1..0000000 --- a/solutions/day-01/countries.js +++ /dev/null @@ -1,15 +0,0 @@ -const countries = [ - 'Albania', - 'Bolivia', - 'Canada', - 'Denmark', - 'Ethiopia', - 'Finland', - 'Germany', - 'Hungary', - 'Ireland', - 'Japan', - 'Kenya', - ] - -export { countries }; \ No newline at end of file diff --git a/solutions/day-01/level2.js b/solutions/day-01/level2.js index ed6f90b..857e3d3 100644 --- a/solutions/day-01/level2.js +++ b/solutions/day-01/level2.js @@ -1,8 +1,29 @@ -import { countries } from "./countries"; -// import { webTechs } from "./web_tech"; +const countries = [ + 'Albania', + 'Bolivia', + 'Canada', + 'Denmark', + 'Ethiopia', + 'Finland', + 'Germany', + 'Hungary', + 'Ireland', + 'Japan', + 'Kenya', + ]; + +const webTechs = [ + 'HTML', + 'CSS', + 'JavaScript', + 'React', + 'Redux', + 'Node', + 'MongoDB', + ] /** - * EXERCISE: Level 2 + * EXERCISE: LEVEL 2 */ // 1. Create a separate countries.js file and store the countries array into this file, @@ -32,4 +53,23 @@ console.log(shoppingCart); // 4. In countries array check if 'Ethiopia' exist in the array if it exists print 'ETHIOPIA'. If it does not exist add // to the countries list -console.log(countries) \ No newline at end of file +if (countries.includes("Ethiopia")) { + console.log('ETHIOPIA'); +} else { + countries.push('Ethiopia'); +} + +// 5. In webTechs array check is 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); +} + +// 6. Concatenate the following two variable and store it in a fullStack variable +const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux']; +const backEnd = ['Node', 'Express', 'MongoDB']; +const fullStack = frontEnd.concat(backEnd); +console.log(fullStack); \ No newline at end of file diff --git a/solutions/day-01/level3.js b/solutions/day-01/level3.js new file mode 100644 index 0000000..6027055 --- /dev/null +++ b/solutions/day-01/level3.js @@ -0,0 +1,59 @@ +/** + * EXERCISE: LEVEL 3 + */ + +const countries = [ + 'Albania', + 'Bolivia', + 'Canada', + 'Denmark', + 'Ethiopia', + 'Finland', + 'Germany', + 'Hungary', + 'Ireland', + 'Japan', + 'Kenya', + ] + +/** + * 1. The following is an array of 10 students ages. + * - Sort the array and find the min and max age + * - Find the median age(one middle item or two middle items divided by two) + * - Find the average age(all items divided by number of items) + * - Find the range of the ages(max minus min) + * - Compare the value of (min - average) and (max - average), use abs() method + */ +const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24] +ages.sort() + +let min = Math.min(...ages) +let max = Math.max(...ages) + +let median + +if (ages.length % 2 === 0) { + median = (ages[ages.length / 2] + ages[ages.length / 2 - 1]) / 2 +} else { + median = ages[ages.length / 2] +} + +let sum = 0 +for (let i = 0; i < ages.length; i++) { + sum += ages[i] +} +let average = sum / ages.length + +let range = Math.max(...ages) - Math.min(...ages) + +// 2. Slice the first ten coutries from the countries array +let firstTen = countries.slice(10) + +// 3. Find the middle countries in the countries array +let middleCountries = countries[Math.floor(countries.length / 2)] + +// 4. Divide the countries array into two equal arrays if it is even. If countries is not even, one more country for the first half +let middle = Math.floor((countries.length - 1) / 2) +const firstHalf = countries.splice(0, middle) +const secondHalf = countries.splice(-middle) + diff --git a/solutions/day-01/web_tech.js b/solutions/day-01/web_tech.js deleted file mode 100644 index 2b3133a..0000000 --- a/solutions/day-01/web_tech.js +++ /dev/null @@ -1,11 +0,0 @@ -const webTechs = [ - 'HTML', - 'CSS', - 'JavaScript', - 'React', - 'Redux', - 'Node', - 'MongoDB', - ]; - - export { webTechs }; \ No newline at end of file diff --git a/solutions/index.html b/solutions/index.html new file mode 100644 index 0000000..98ac388 --- /dev/null +++ b/solutions/index.html @@ -0,0 +1,9 @@ + + + + Solutions Tester + + + + + \ No newline at end of file