From 11fda60bb9e7315466069c4a6f3c8298689b6b81 Mon Sep 17 00:00:00 2001 From: Helder Cambuta Date: Fri, 24 Feb 2023 18:23:50 +0100 Subject: [PATCH] :sparkles: fix: Resolved issues involving arrays --- .../solutions/arrays/{ => level1}/level1.js | 0 .../solutions/arrays/level2/countries.js | 13 ++ .../solutions/arrays/level2/main.js | 49 +++++ .../solutions/arrays/level2/web_techs.js | 9 + .../solutions/arrays/level3/countries.js | 195 ++++++++++++++++++ .../solutions/arrays/level3/level3.js | 48 +++++ 6 files changed, 314 insertions(+) rename 01_Day_JavaScript_Refresher/solutions/arrays/{ => level1}/level1.js (100%) create mode 100644 01_Day_JavaScript_Refresher/solutions/arrays/level2/countries.js create mode 100644 01_Day_JavaScript_Refresher/solutions/arrays/level2/main.js create mode 100644 01_Day_JavaScript_Refresher/solutions/arrays/level2/web_techs.js create mode 100644 01_Day_JavaScript_Refresher/solutions/arrays/level3/countries.js create mode 100644 01_Day_JavaScript_Refresher/solutions/arrays/level3/level3.js diff --git a/01_Day_JavaScript_Refresher/solutions/arrays/level1.js b/01_Day_JavaScript_Refresher/solutions/arrays/level1/level1.js similarity index 100% rename from 01_Day_JavaScript_Refresher/solutions/arrays/level1.js rename to 01_Day_JavaScript_Refresher/solutions/arrays/level1/level1.js diff --git a/01_Day_JavaScript_Refresher/solutions/arrays/level2/countries.js b/01_Day_JavaScript_Refresher/solutions/arrays/level2/countries.js new file mode 100644 index 0000000..b412305 --- /dev/null +++ b/01_Day_JavaScript_Refresher/solutions/arrays/level2/countries.js @@ -0,0 +1,13 @@ +module.exports = countries = [ + 'Albania', + 'Bolivia', + 'Canada', + 'Denmark', + 'Ethiopia', + 'Finland', + 'Germany', + 'Hungary', + 'Ireland', + 'Japan', + 'Kenya', +] \ No newline at end of file diff --git a/01_Day_JavaScript_Refresher/solutions/arrays/level2/main.js b/01_Day_JavaScript_Refresher/solutions/arrays/level2/main.js new file mode 100644 index 0000000..f26a8cc --- /dev/null +++ b/01_Day_JavaScript_Refresher/solutions/arrays/level2/main.js @@ -0,0 +1,49 @@ +const countries = require('./countries') +const webTechs = require("./web_techs") + +console.log(countries) +console.log(webTechs) + + +let text = 'I love teaching and empowering people. I teach HTML, CSS, JS, React, Python.' +let words = text.replace('/[,]+/g', ' ').split(' ') + +console.log(words) +console.log(words.length) + + +const shoppingCart = ['Milk', 'Coffee', 'Tea', 'Honey'] + +shoppingCart.includes('Meat') ? '' : shoppingCart.push('Meat') +shoppingCart.includes('Sugar') ? '' : shoppingCart.push('Sugar') + + + +let alergic = true +if (alergic) { + let index = shoppingCart.indexOf('Honey') + shoppingCart.splice(index, 1) +} + + +if (countries.includes('Ethiopia')) { + let index = countries.indexOf('Ethiopia') + console.log(countries[index].toUpperCase()) +} else { + countries.push('Ethiopia') +} + + +if (webTechs.includes('Sass')) { + console.log('Sass is a CSS preprocess') +} else { + webTechs.push('Sass') + console.log(webTechs) +} + + +const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux'] +const backEnd = ['Node', 'Express', 'MongoDB'] + +const fullStack = [...frontEnd, ...backEnd] +console.log(fullStack) diff --git a/01_Day_JavaScript_Refresher/solutions/arrays/level2/web_techs.js b/01_Day_JavaScript_Refresher/solutions/arrays/level2/web_techs.js new file mode 100644 index 0000000..c724697 --- /dev/null +++ b/01_Day_JavaScript_Refresher/solutions/arrays/level2/web_techs.js @@ -0,0 +1,9 @@ +module.exports = webTechs = [ + 'HTML', + 'CSS', + 'JavaScript', + 'React', + 'Redux', + 'Node', + 'MongoDB', +] \ No newline at end of file diff --git a/01_Day_JavaScript_Refresher/solutions/arrays/level3/countries.js b/01_Day_JavaScript_Refresher/solutions/arrays/level3/countries.js new file mode 100644 index 0000000..756e229 --- /dev/null +++ b/01_Day_JavaScript_Refresher/solutions/arrays/level3/countries.js @@ -0,0 +1,195 @@ +module.exports = countries = [ + 'Afghanistan', + 'Albania', + 'Algeria', + 'Andorra', + 'Angola', + 'Antigua and Barbuda', + 'Argentina', + 'Armenia', + 'Australia', + 'Austria', + 'Azerbaijan', + 'Bahamas', + 'Bahrain', + 'Bangladesh', + 'Barbados', + 'Belarus', + 'Belgium', + 'Belize', + 'Benin', + 'Bhutan', + 'Bolivia', + 'Bosnia and Herzegovina', + 'Botswana', + 'Brazil', + 'Brunei', + 'Bulgaria', + 'Burkina Faso', + 'Burundi', + 'Cambodia', + 'Cameroon', + 'Canada', + 'Cape Verde', + 'Central African Republic', + 'Chad', + 'Chile', + 'China', + 'Colombi', + 'Comoros', + 'Congo (Brazzaville)', + 'Congo', + 'Costa Rica', + "Cote d'Ivoire", + 'Croatia', + 'Cuba', + 'Cyprus', + 'Czech Republic', + 'Denmark', + 'Djibouti', + 'Dominica', + 'Dominican Republic', + 'East Timor (Timor Timur)', + 'Ecuador', + 'Egypt', + 'El Salvador', + 'Equatorial Guinea', + 'Eritrea', + 'Estonia', + 'Ethiopia', + 'Fiji', + 'Finland', + 'France', + 'Gabon', + 'Gambia, The', + 'Georgia', + 'Germany', + 'Ghana', + 'Greece', + 'Grenada', + 'Guatemala', + 'Guinea', + 'Guinea-Bissau', + 'Guyana', + 'Haiti', + 'Honduras', + 'Hungary', + 'Iceland', + 'India', + 'Indonesia', + 'Iran', + 'Iraq', + 'Ireland', + 'Israel', + 'Italy', + 'Jamaica', + 'Japan', + 'Jordan', + 'Kazakhstan', + 'Kenya', + 'Kiribati', + 'Korea, North', + 'Korea, South', + 'Kuwait', + 'Kyrgyzstan', + 'Laos', + 'Latvia', + 'Lebanon', + 'Lesotho', + 'Liberia', + 'Libya', + 'Liechtenstein', + 'Lithuania', + 'Luxembourg', + 'Macedonia', + 'Madagascar', + 'Malawi', + 'Malaysia', + 'Maldives', + 'Mali', + 'Malta', + 'Marshall Islands', + 'Mauritania', + 'Mauritius', + 'Mexico', + 'Micronesia', + 'Moldova', + 'Monaco', + 'Mongolia', + 'Morocco', + 'Mozambique', + 'Myanmar', + 'Namibia', + 'Nauru', + 'Nepal', + 'Netherlands', + 'New Zealand', + 'Nicaragua', + 'Niger', + 'Nigeria', + 'Norway', + 'Oman', + 'Pakistan', + 'Palau', + 'Panama', + 'Papua New Guinea', + 'Paraguay', + 'Peru', + 'Philippines', + 'Poland', + 'Portugal', + 'Qatar', + 'Romania', + 'Russia', + 'Rwanda', + 'Saint Kitts and Nevis', + 'Saint Lucia', + 'Saint Vincent', + 'Samoa', + 'San Marino', + 'Sao Tome and Principe', + 'Saudi Arabia', + 'Senegal', + 'Serbia and Montenegro', + 'Seychelles', + 'Sierra Leone', + 'Singapore', + 'Slovakia', + 'Slovenia', + 'Solomon Islands', + 'Somalia', + 'South Africa', + 'Spain', + 'Sri Lanka', + 'Sudan', + 'Suriname', + 'Swaziland', + 'Sweden', + 'Switzerland', + 'Syria', + 'Taiwan', + 'Tajikistan', + 'Tanzania', + 'Thailand', + 'Togo', + 'Tonga', + 'Trinidad and Tobago', + 'Tunisia', + 'Turkey', + 'Turkmenistan', + 'Tuvalu', + 'Uganda', + 'Ukraine', + 'United Arab Emirates', + 'United Kingdom', + 'United States', + 'Uruguay', + 'Uzbekistan', + 'Vanuatu', + 'Vatican City', + 'Venezuela', + 'Vietnam', + 'Yemen', + 'Zambia', + 'Zimbabwe' +] \ No newline at end of file diff --git a/01_Day_JavaScript_Refresher/solutions/arrays/level3/level3.js b/01_Day_JavaScript_Refresher/solutions/arrays/level3/level3.js new file mode 100644 index 0000000..c938a6b --- /dev/null +++ b/01_Day_JavaScript_Refresher/solutions/arrays/level3/level3.js @@ -0,0 +1,48 @@ +// 1. 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 +// - 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 age = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24] + +let min = Math.min(...age) +let max = Math.max(...age) + +age.sort() +let [m1, m2] = [(age.length / 2), (age.length / 2 + 1)] +let median = (age[m1] + age[m2]) / 2 + +let average = age.reduce((x, y) => { + return x + y +}, 0) / age.length + +let range = max - min + + +// 1. Slice the first ten countries from the countries array +const countries = require('./countries') + +let firstTen = countries.slice(0, 9) + +// 2. Find the middle country(ies) in the countries array +let middle = Math.floor(countries.length / 2) + + +// 3. 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. +let arr1, arr2 = [] + +if (countries.length % 2 == 0) { + let middle = (countries.length / 2) + + arr1 = countries.slice(0, middle) + arr2 = countries.slice(middle, countries.length) +} +else { + let middle = Math.ceil(countries.length / 2) + + arr1 = countries.slice(0, middle) + arr2 = countries.slice(middle, countries.length) +}