From 3305d7cad3423249420c3cb46861bc33e74c0b9f Mon Sep 17 00:00:00 2001 From: Frank Ojwang Date: Fri, 9 May 2025 02:13:24 +0300 Subject: [PATCH] LEVEL 3 complete --- .../05_day_starter/data/countries.mjs | 386 +++++++++--------- 05_Day_Arrays/05_day_starter/scripts/main.mjs | 105 ++++- 2 files changed, 281 insertions(+), 210 deletions(-) diff --git a/05_Day_Arrays/05_day_starter/data/countries.mjs b/05_Day_Arrays/05_day_starter/data/countries.mjs index 2207780..c6aba66 100644 --- a/05_Day_Arrays/05_day_starter/data/countries.mjs +++ b/05_Day_Arrays/05_day_starter/data/countries.mjs @@ -1,195 +1,195 @@ export const 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', + "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' -] + "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/05_Day_Arrays/05_day_starter/scripts/main.mjs b/05_Day_Arrays/05_day_starter/scripts/main.mjs index abb88ba..9021ed0 100644 --- a/05_Day_Arrays/05_day_starter/scripts/main.mjs +++ b/05_Day_Arrays/05_day_starter/scripts/main.mjs @@ -35,8 +35,8 @@ const itCompanies = [ "Apple", "IBM", "Oracle", - "Amazon", - "Netflix" + "Amazon", + "Netflix", ]; //Print the array using console.log() @@ -74,7 +74,7 @@ let companySearch = "IBAM"; // console.log("Companies with double o") let doubleo = []; for (let company of itCompanies) { - company.match(/oo/gm) ? doubleo.push(company) : null + company.match(/oo/gm) ? doubleo.push(company) : null; } // console.log(doubleo); @@ -135,45 +135,116 @@ let words = text.replace(/\,|\./gm, "").split(" "); // console.log(`Text Length: ${words.length}`); //In the following shopping cart add, remove, edit items -const shoppingCart = ['Milk', 'Coffee', 'Tea', 'Honey']; +const shoppingCart = ["Milk", "Coffee", "Tea", "Honey"]; //Add 'Meat' in the beginning of your shoppinh cart if it has not been already added let foodToAdd = "Meat"; let checkFood = shoppingCart.includes(foodToAdd); -!checkFood ? shoppingCart.unshift(foodToAdd) : ''; -console.log(shoppingCart); +!checkFood ? shoppingCart.unshift(foodToAdd) : ""; +// console.log(shoppingCart); //Add Sugar at the end of your shopping cart if it has not already been added let foodToAdd2 = "Sugar"; let checkFood2 = shoppingCart.includes(foodToAdd2); !checkFood2 ? shoppingCart.push(foodToAdd2) : ""; -console.log(shoppingCart); +// console.log(shoppingCart); //Remove Honey if you are allergic to honey -let foodToRemove = "Honey" +let foodToRemove = "Honey"; let allergy = shoppingCart.includes(foodToRemove); allergy ? shoppingCart.splice(shoppingCart.indexOf(foodToRemove), 1) : null; -console.log(shoppingCart); +// console.log(shoppingCart); //Modify Tea to 'Green Tea' let searchItem = "Tea"; let checkItem = shoppingCart.includes(searchItem); -checkItem ? shoppingCart[shoppingCart.indexOf(searchItem)] = "Green Tea" : null; -console.log(shoppingCart); +checkItem + ? (shoppingCart[shoppingCart.indexOf(searchItem)] = "Green Tea") + : null; +// console.log(shoppingCart); //Check if Ethiopia exists in the countries array. If it exists, print 'ETHIOPIA'. If not, add it to the countries list let checkCountries = countries.includes("Ethiopia"); -checkCountries ? console.log('ETHIOPIA') : countries.push('Ethiopia'); +checkCountries ? console.log("ETHIOPIA") : countries.push("Ethiopia"); //Check if Sass exists in webTechs, if it exists, print 'Sass is a CSS Preprocessor'. If not, add Sass to webTechs let checkWebTechs = webTechs.includes("Sass"); -checkWebTechs ? console.log("Sass is a CSS Preprocessor") : webTechs.push("Sass"); -console.log(webTechs); +checkWebTechs + ? console.log("Sass is a CSS Preprocessor") + : 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 frontEnd = ["HTML", "CSS", "JS", "React", "Redux"]; +const backend = ["Node", "Express", "MongoDB"]; let fullStack = frontEnd.concat(backend); -console.log(fullStack); \ No newline at end of file +// console.log(fullStack); + +//LEVEL 3 + +//sort array of student ages +const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24]; + +ages.sort((a, b) => a - b); + +console.log(ages); + +//Find the median age (one middle item or two middle items divided by two) +let median = + ages.length % 2 > 0 + ? ages[Math.floor(ages.length / 2)] + : Math.round((ages[ages.length / 2 - 1] + ages[ages.length / 2]) / 2); +// console.log(median) + +//Find the average age (all items divided by number of items) +let sumAges = 0; +ages.forEach((age) => (sumAges += age)); +let average = sumAges / ages.length; +// console.log(average); + +//Find the range of the ages (max minus min) +let minAge = Math.min(...ages); +let maxAge = Math.max(...ages); +//using arrays after sorting +//let minAge = ages[0] +//let maxAge = ages.reverse()[0]; +let range = maxAge - minAge; +// console.log(range) + +//Compare the value of (min - average) and (max - average), use abs() method +let minval = minAge - average; +let maxval = maxAge - average; +console.log(Math.abs(maxval - minval)); + +//Slice the first ten countries from countries array +// console.log(countries.slice(0, 10)); + +//Find the middle country(ies) in the countries array +countries.length % 2 !== 0 + ? console.log(countries[Math.ceil(countries.length / 2) - 1]) + : console.log( + countries[Math.floor(countries.length / 2 - 2)], + countries[Math.floor(countries.length / 2 - 1)] + ); + +//Divide the countries array into two equal arrays if it is even. if not, add one more country to the first half + +let firstHalf = []; +let secondHalf = []; + +let isEven = countries.length % 2 !== 0 ? false : true; + +isEven + ? (firstHalf.push(...countries.slice(0, countries.length / 2)), + secondHalf.push(...countries.slice(countries.length / 2, countries.length))) + : (firstHalf.push(...countries.slice(0, Math.ceil(countries.length / 2))), + secondHalf.push( + ...countries.slice(Math.ceil(countries.length / 2), countries.length) + )); + +console.log(isEven); +console.log(countries.length); +console.log(firstHalf.length, firstHalf); +console.log(secondHalf.length, secondHalf);