From 8c9b24b2e05baac45b76ce1a0723890e4c7d8950 Mon Sep 17 00:00:00 2001 From: Brendan Klein Date: Sun, 10 Sep 2023 17:13:33 -0400 Subject: [PATCH] chore: Finished Day 4 exercises (Levels 1,2, and 3) - Finished all exercises for day 4, Levels 1,2, and 3 - Day 4 (Arrays) - Complete --- 05_Day_Arrays/Exercises/data/countries.js | 195 +++++++++++++ 05_Day_Arrays/Exercises/data/web_techs.js | 8 + 05_Day_Arrays/Exercises/index.html | 18 ++ 05_Day_Arrays/Exercises/scripts/main.js | 331 ++++++++++++++++++++++ 4 files changed, 552 insertions(+) create mode 100644 05_Day_Arrays/Exercises/data/countries.js create mode 100644 05_Day_Arrays/Exercises/data/web_techs.js create mode 100644 05_Day_Arrays/Exercises/index.html create mode 100644 05_Day_Arrays/Exercises/scripts/main.js diff --git a/05_Day_Arrays/Exercises/data/countries.js b/05_Day_Arrays/Exercises/data/countries.js new file mode 100644 index 0000000..e57b005 --- /dev/null +++ b/05_Day_Arrays/Exercises/data/countries.js @@ -0,0 +1,195 @@ +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', + "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' +] diff --git a/05_Day_Arrays/Exercises/data/web_techs.js b/05_Day_Arrays/Exercises/data/web_techs.js new file mode 100644 index 0000000..0ccde5f --- /dev/null +++ b/05_Day_Arrays/Exercises/data/web_techs.js @@ -0,0 +1,8 @@ +const webTechs = [ + 'HTML', + 'CSS', + 'JS', + 'React', + 'Redux', + 'Node', + 'MongDB'] // array of web technologies \ No newline at end of file diff --git a/05_Day_Arrays/Exercises/index.html b/05_Day_Arrays/Exercises/index.html new file mode 100644 index 0000000..7d4d2f4 --- /dev/null +++ b/05_Day_Arrays/Exercises/index.html @@ -0,0 +1,18 @@ + + + + + 30DaysOfJavaScript:05 Day + + + +

30DaysOfJavaScript:05 Day

+

Arrays

+ + + + + + + + \ No newline at end of file diff --git a/05_Day_Arrays/Exercises/scripts/main.js b/05_Day_Arrays/Exercises/scripts/main.js new file mode 100644 index 0000000..c51cfc3 --- /dev/null +++ b/05_Day_Arrays/Exercises/scripts/main.js @@ -0,0 +1,331 @@ +console.log(countries) + +//Exercises Level 1 + + //Exercise 1 -- Declares an empty array + var emptyArray = []; + + //Exercise 2 -- Declare an array with more than 5 elements + var names = ["Brendan", "Jacob", "Chloe", "Katherine", "Nicole", "Sienna"]; + + //Exercise 3 -- Find the length of your array in #2 and prints it to console + var lengthOfArray = names.length + console.log(`Length of names array is: ${lengthOfArray}`); + + //Exercise 4 -- Gets the first, middle, and last item of the array from exercise 3 and print the result to console + var firstElement = names[0]; + var middleElement; + + console.log(`First element: ${firstElement}`); + + (names.length%2==0) + ?console.log(`Middle elements are ${names[Math.floor((names.length-1)/2)]} and ${names[Math.ceil((names.length-1)/2)]}`) //value is true + :console.log(`${names[((names.length-1)/2)]}`); //value is false + + var lastElement = names[names.length-1]; + + console.log(`Last element: ${lastElement}`); + + //Exercise 5 -- 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 + var mixedDataTypes = [1, 2, 3, "myString", "yourString", true, false]; + + console.log(`Length of mixedDataTypes is ${mixedDataTypes.length}`); + + //Exercise 6 -- Declares an array named itCompanies and assigned initial values Facebook, Google, Microsoft, Apple, IBM, Oracle and Amazon + var itCompanies = ["Facebook","Google","Microsoft","Apple","IBM","Oracle","Amazon"]; + + //Exercise 7 -- Prints the array using console.log() + console.log(itCompanies); + + //Exercise 8 -- Prints the number of companies in the itCompanies array + console.log(`Number of itCompanies is ${itCompanies.length}`); + + //Exercise 9 -- Prints the first company, middle and last company (from itCompanies) + var first = itCompanies[0]; + + var middle; + (itCompanies.length%2==0) + ?console.log(`Middle elements are ${itCompanies[Math.floor((itCompanies.length-1)/2)]} and ${itCompanies[Math.ceil((itCompanies.length-1)/2)]}`) //value is true + :console.log(`${itCompanies[((itCompanies.length-1)/2)]}`); //value is false + + var last = itCompanies[itCompanies.length-1]; + + //Exercise 10 -- Prints out each company + for(var i=0;i=0) + console.log(`${itCompanies[tempValue]}`); + else + console.log('The company is not found'); + + //Exercise 14 -- Filter out companies which have more than one 'o' without the filter method + var tempItCompanies = []; + var filterCounter = 0; + + for(var a=0; a=0) + console.log(`${(countries[tempIndex]).toUpperCase()}`); //Prints country if found in countries array + else + { + countries.push('Ethiopia'); //Adds to end of array if not found + console.log(countries); + } + + //Exercise 5 -- 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. + //Uses searchStringInArray function from Exercises Level 1, Exercise 13 and tempIndex from Exercises Level 2, Exercise 4 + tempIndex = searchStringInArray('Sass', webTechs); + + if(tempIndex>=0) + console.log('Sass is a CSS preprocess'); + else + { + webTechs.push('Sass'); + console.log(webTechs); + } + + //Exercise 6 -- Concatenate the given two variables and store it in a fullStack variable, and prints fullStack to console + const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux']; + const backEnd = ['Node','Express', 'MongoDB']; + const fullStack = frontEnd.concat(backEnd); + + console.log(fullStack); + +//Exercises Level 3 + + //Exercise 1 -- The following is an array of 10 students ages: + const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24]; + + // Sort the array and find the min and max age + ages.sort(); + console.log(`Min age of students: ${ages[0]} Max age of students: ${ages[ages.length-1]}`); + + // Find the median age(one middle item or two middle items divided by two) + var middleValueHolder = ((ages.length-1)/2); + var median = 0; + + if(ages.length % 2 == 0) + median = ((ages[Math.floor(middleValueHolder)]+ages[Math.ceil(middleValueHolder)])/2); + else + median = ages[Math.floor((ages.length-1)/2)]; //Tested and fully working + + console.log(median); + + // Find the average age(all items divided by number of items) + var numAgeElements = ages.length; + var averageAge = 0; + + for(var b=0; b