From ccf68ac5e8047c25726fc3aeb4f019ea159b2220 Mon Sep 17 00:00:00 2001 From: Frank Ojwang Date: Tue, 29 Apr 2025 03:15:58 +0300 Subject: [PATCH 1/5] 05-Finished Level 1 --- 05_Day_Arrays/05_day_starter/scripts/main.js | 142 ++++++++++++++++++- 1 file changed, 139 insertions(+), 3 deletions(-) diff --git a/05_Day_Arrays/05_day_starter/scripts/main.js b/05_Day_Arrays/05_day_starter/scripts/main.js index 50cc07eac..d070ac625 100644 --- a/05_Day_Arrays/05_day_starter/scripts/main.js +++ b/05_Day_Arrays/05_day_starter/scripts/main.js @@ -1,3 +1,139 @@ -console.log(countries) -alert('Open the browser console whenever you work on JavaScript') -alert('Open the console and check if the countries has been loaded') \ No newline at end of file +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; +const emptyArray = []; + +//Declare an array with more than 5 number of elements +const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + +//Find the length of the array +console.log("Length of numbers array:", numbers.length); + +//Get the first, middle and last elements of the array +console.log("First Item:", numbers[0]); +console.log("Middle Item:", numbers[Math.floor(numbers.length / 2)]); +console.log("Last Item:", numbers[numbers.length - 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 +const mixedDataTypes = [ + 1, + "string", + true, + null, + undefined, + { key: "value" }, + [1, 2, 3], +]; + +//Declare an array variable name it itCompanies and assign initial values +const itCompanies = [ + "Facebook", + "Google", + "Microsoft", + "Apple", + "IBM", + "Oracle", + "Amazon", + "Netflix" +]; + +//Print the array using console.log() +console.log("IT Companies:", itCompanies); + +//Print the number of companies in the array +console.log("IT Companies count:", itCompanies.length); + +//Print the first, middle and last IT company +console.log("First IT Company:", itCompanies[0]); +console.log( + "Middle IT Company:", + itCompanies[Math.floor(itCompanies.length / 2)] +); +console.log("Last IT Company:", itCompanies[itCompanies.length - 1]); + +//Print out each company +console.log("IT Companies List:"); +itCompanies.forEach((company) => console.log(company)); + +//Change each company name to uppercase one by one and print them out +console.log("Companies in Uppercase"); +itCompanies.forEach((company) => console.log(company.toUpperCase())); + +//Print out the Array as a sentence +console.log(itCompanies.toString()); + +//Check if a certain company exists in the itCompanies array. If it exists, return the company else return a company is not found +let companySearch = "IBAM"; +itCompanies.includes(companySearch) == true + ? console.log(companySearch, "has been found") + : console.log(`The company ${companySearch} is not found`); + +//Filter out companies which have more than one "o" without the filter method +console.log("Companies with double o") +let doubleo = []; +for (let company of itCompanies) { + company.match(/oo/gm) ? doubleo.push(company) : null +} +console.log(doubleo); + +//Sort the array using sort() method +console.log(itCompanies.sort()); + +//Reverse the array using the reverse() method +// console.log(itCompanies.reverse()); + +//Slice out the first 3 companies from the array +// itCompanies.splice(0, 3); +// console.log(itCompanies) + +//Slice out the last 3 companies from the array +// itCompanies.splice(itCompanies.length - 3, 3); +console.log(itCompanies) + +//Slice out the middle IT company or companies from the array +// if (itCompanies.length % 2 > 0) { +// itCompanies.splice(Math.floor(itCompanies.length / 2), 1); +// } else { +// itCompanies.splice(Math.floor(itCompanies.length / 2) - 1, 2); +// } +// console.log(itCompanies) + +//Remove the first IT company from the array +// itCompanies.shift(); +// console.log(itCompanies) + +//Remove the middle IT company or companies from the array +// if (itCompanies.length % 2 > 0) { +// itCompanies.splice(Math.floor(itCompanies.length / 2), 1); +// } else { +// itCompanies.splice(Math.floor(itCompanies.length / 2) - 1, 2); +// } +// console.log(itCompanies) + +//Remove the last IT company from the array +// itCompanies.pop(); +// console.log(itCompanies); + +//Remove all the IT companies +console.log(itCompanies.splice()); \ No newline at end of file From 204931a9993c3ec6160e18ec25de8d47ccd6de3e Mon Sep 17 00:00:00 2001 From: Frank Ojwang Date: Tue, 29 Apr 2025 23:58:41 +0300 Subject: [PATCH 2/5] Level 2 - Halfway done --- .vscode/settings.json | 8 ++ .../data/{countries.js => countries.mjs} | 2 +- .../05_day_starter/data/webtechs.mjs | 9 ++ 05_Day_Arrays/05_day_starter/index.html | 3 +- 05_Day_Arrays/05_day_starter/scripts/main.js | 90 +++++++++---------- 5 files changed, 63 insertions(+), 49 deletions(-) create mode 100644 .vscode/settings.json rename 05_Day_Arrays/05_day_starter/data/{countries.js => countries.mjs} (99%) create mode 100644 05_Day_Arrays/05_day_starter/data/webtechs.mjs diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..eecbcdc23 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "github.copilot.enable": { + "*": false, + "plaintext": false, + "markdown": false, + "scminput": false + } +} \ No newline at end of file diff --git a/05_Day_Arrays/05_day_starter/data/countries.js b/05_Day_Arrays/05_day_starter/data/countries.mjs similarity index 99% rename from 05_Day_Arrays/05_day_starter/data/countries.js rename to 05_Day_Arrays/05_day_starter/data/countries.mjs index e57b00501..220778077 100644 --- a/05_Day_Arrays/05_day_starter/data/countries.js +++ b/05_Day_Arrays/05_day_starter/data/countries.mjs @@ -1,4 +1,4 @@ -const countries = [ +export const countries = [ 'Afghanistan', 'Albania', 'Algeria', diff --git a/05_Day_Arrays/05_day_starter/data/webtechs.mjs b/05_Day_Arrays/05_day_starter/data/webtechs.mjs new file mode 100644 index 000000000..63d7ff50a --- /dev/null +++ b/05_Day_Arrays/05_day_starter/data/webtechs.mjs @@ -0,0 +1,9 @@ +export const webTechs = [ + "HTML", + "CSS", + "JavaScript", + "React", + "Redux", + "Node", + "MongoDB", +]; diff --git a/05_Day_Arrays/05_day_starter/index.html b/05_Day_Arrays/05_day_starter/index.html index 7d8eeea5b..819337df6 100644 --- a/05_Day_Arrays/05_day_starter/index.html +++ b/05_Day_Arrays/05_day_starter/index.html @@ -10,7 +10,8 @@

30DaysOfJavaScript:05 Day

Arrays

- + + diff --git a/05_Day_Arrays/05_day_starter/scripts/main.js b/05_Day_Arrays/05_day_starter/scripts/main.js index d070ac625..936dc33a8 100644 --- a/05_Day_Arrays/05_day_starter/scripts/main.js +++ b/05_Day_Arrays/05_day_starter/scripts/main.js @@ -1,25 +1,6 @@ -const countries = [ - "Albania", - "Bolivia", - "Canada", - "Denmark", - "Ethiopia", - "Finland", - "Germany", - "Hungary", - "Ireland", - "Japan", - "Kenya", -]; -const webTechs = [ - "HTML", - "CSS", - "JavaScript", - "React", - "Redux", - "Node", - "MongoDB", -]; +// import { webTechs } from "../data/webtechs"; +// import { countries } from "../data/countries.mjs"; +// import { webTechs } from "../data/webtechs.mjs"; //Declare an empty array; const emptyArray = []; @@ -28,12 +9,12 @@ const emptyArray = []; const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; //Find the length of the array -console.log("Length of numbers array:", numbers.length); +// console.log("Length of numbers array:", numbers.length); //Get the first, middle and last elements of the array -console.log("First Item:", numbers[0]); -console.log("Middle Item:", numbers[Math.floor(numbers.length / 2)]); -console.log("Last Item:", numbers[numbers.length - 1]); +// console.log("First Item:", numbers[0]); +// console.log("Middle Item:", numbers[Math.floor(numbers.length / 2)]); +// console.log("Last Item:", numbers[numbers.length - 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 const mixedDataTypes = [ @@ -59,46 +40,46 @@ const itCompanies = [ ]; //Print the array using console.log() -console.log("IT Companies:", itCompanies); +// console.log("IT Companies:", itCompanies); //Print the number of companies in the array -console.log("IT Companies count:", itCompanies.length); +// console.log("IT Companies count:", itCompanies.length); //Print the first, middle and last IT company -console.log("First IT Company:", itCompanies[0]); -console.log( - "Middle IT Company:", - itCompanies[Math.floor(itCompanies.length / 2)] -); -console.log("Last IT Company:", itCompanies[itCompanies.length - 1]); +// console.log("First IT Company:", itCompanies[0]); +// console.log( +// "Middle IT Company:", +// itCompanies[Math.floor(itCompanies.length / 2)] +// ); +// console.log("Last IT Company:", itCompanies[itCompanies.length - 1]); //Print out each company -console.log("IT Companies List:"); -itCompanies.forEach((company) => console.log(company)); +// console.log("IT Companies List:"); +// itCompanies.forEach((company) => console.log(company)); //Change each company name to uppercase one by one and print them out -console.log("Companies in Uppercase"); -itCompanies.forEach((company) => console.log(company.toUpperCase())); +// console.log("Companies in Uppercase"); +// itCompanies.forEach((company) => console.log(company.toUpperCase())); //Print out the Array as a sentence -console.log(itCompanies.toString()); +// console.log(itCompanies.toString()); //Check if a certain company exists in the itCompanies array. If it exists, return the company else return a company is not found let companySearch = "IBAM"; -itCompanies.includes(companySearch) == true - ? console.log(companySearch, "has been found") - : console.log(`The company ${companySearch} is not found`); +// itCompanies.includes(companySearch) == true +// ? console.log(companySearch, "has been found") +// : console.log(`The company ${companySearch} is not found`); //Filter out companies which have more than one "o" without the filter method -console.log("Companies with double o") +// console.log("Companies with double o") let doubleo = []; for (let company of itCompanies) { company.match(/oo/gm) ? doubleo.push(company) : null } -console.log(doubleo); +// console.log(doubleo); //Sort the array using sort() method -console.log(itCompanies.sort()); +// console.log(itCompanies.sort()); //Reverse the array using the reverse() method // console.log(itCompanies.reverse()); @@ -109,7 +90,7 @@ console.log(itCompanies.sort()); //Slice out the last 3 companies from the array // itCompanies.splice(itCompanies.length - 3, 3); -console.log(itCompanies) +// console.log(itCompanies) //Slice out the middle IT company or companies from the array // if (itCompanies.length % 2 > 0) { @@ -136,4 +117,19 @@ console.log(itCompanies) // console.log(itCompanies); //Remove all the IT companies -console.log(itCompanies.splice()); \ No newline at end of file +// console.log(itCompanies.splice()); + +//LEVEL 2 + +//Create separate files for countries and web_techs and access from this file. +// console.log(countries) +// console.log(webTechs); + +//Remove punctuations and change the string to array and count the number of words in it. + +let text = + "I love teaching and empowering people. I teach HTML, CSS, JS, React, Python."; + +let words = text.replace(/\,|\./gm, "").split(" "); +console.log("Text Array:", words) +console.log(`Text Length: ${words.length}`); From 39fb8a037a80aaecd8a4a47a7dd4991a673bc0ff Mon Sep 17 00:00:00 2001 From: Frank Ojwang Date: Wed, 30 Apr 2025 01:25:15 +0300 Subject: [PATCH 3/5] Level 2: Completed level 2 --- .../scripts/{main.js => main.mjs} | 52 +++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) rename 05_Day_Arrays/05_day_starter/scripts/{main.js => main.mjs} (67%) diff --git a/05_Day_Arrays/05_day_starter/scripts/main.js b/05_Day_Arrays/05_day_starter/scripts/main.mjs similarity index 67% rename from 05_Day_Arrays/05_day_starter/scripts/main.js rename to 05_Day_Arrays/05_day_starter/scripts/main.mjs index 936dc33a8..abb88ba76 100644 --- a/05_Day_Arrays/05_day_starter/scripts/main.js +++ b/05_Day_Arrays/05_day_starter/scripts/main.mjs @@ -1,6 +1,6 @@ // import { webTechs } from "../data/webtechs"; -// import { countries } from "../data/countries.mjs"; -// import { webTechs } from "../data/webtechs.mjs"; +import { countries } from "../data/countries.mjs"; +import { webTechs } from "../data/webtechs.mjs"; //Declare an empty array; const emptyArray = []; @@ -131,5 +131,49 @@ let text = "I love teaching and empowering people. I teach HTML, CSS, JS, React, Python."; let words = text.replace(/\,|\./gm, "").split(" "); -console.log("Text Array:", words) -console.log(`Text Length: ${words.length}`); +// console.log("Text Array:", words) +// console.log(`Text Length: ${words.length}`); + +//In the following shopping cart add, remove, edit items +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); + +//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); + +//Remove Honey if you are allergic to honey +let foodToRemove = "Honey" +let allergy = shoppingCart.includes(foodToRemove); +allergy ? shoppingCart.splice(shoppingCart.indexOf(foodToRemove), 1) : null; +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); + +//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'); + +//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); + +//Concatenate the following two variables and store it in a fullStack variable +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 From 3305d7cad3423249420c3cb46861bc33e74c0b9f Mon Sep 17 00:00:00 2001 From: Frank Ojwang Date: Fri, 9 May 2025 02:13:24 +0300 Subject: [PATCH 4/5] 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 220778077..c6aba664c 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 abb88ba76..9021ed04c 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); From c02c846d2f9e7d70a4a0785f52c0c2609d979e76 Mon Sep 17 00:00:00 2001 From: Frank Ojwang Date: Fri, 9 May 2025 02:21:00 +0300 Subject: [PATCH 5/5] log to update remote origin --- 05_Day_Arrays/05_day_starter/scripts/main.mjs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/05_Day_Arrays/05_day_starter/scripts/main.mjs b/05_Day_Arrays/05_day_starter/scripts/main.mjs index 9021ed04c..ac3a57b87 100644 --- a/05_Day_Arrays/05_day_starter/scripts/main.mjs +++ b/05_Day_Arrays/05_day_starter/scripts/main.mjs @@ -248,3 +248,5 @@ console.log(isEven); console.log(countries.length); console.log(firstHalf.length, firstHalf); console.log(secondHalf.length, secondHalf); + +console.log("New change") \ No newline at end of file