LEVEL 3 complete

pull/990/head
Frank Ojwang 4 months ago
parent 39fb8a037a
commit 3305d7cad3

@ -1,195 +1,195 @@
export const countries = [ export const countries = [
'Afghanistan', "Afghanistan",
'Albania', "Albania",
'Algeria', "Algeria",
'Andorra', "Andorra",
'Angola', "Angola",
'Antigua and Barbuda', "Antigua and Barbuda",
'Argentina', "Argentina",
'Armenia', "Armenia",
'Australia', "Australia",
'Austria', "Austria",
'Azerbaijan', "Azerbaijan",
'Bahamas', "Bahamas",
'Bahrain', "Bahrain",
'Bangladesh', "Bangladesh",
'Barbados', "Barbados",
'Belarus', "Belarus",
'Belgium', "Belgium",
'Belize', "Belize",
'Benin', "Benin",
'Bhutan', "Bhutan",
'Bolivia', "Bolivia",
'Bosnia and Herzegovina', "Bosnia and Herzegovina",
'Botswana', "Botswana",
'Brazil', "Brazil",
'Brunei', "Brunei",
'Bulgaria', "Bulgaria",
'Burkina Faso', "Burkina Faso",
'Burundi', "Burundi",
'Cambodia', "Cambodia",
'Cameroon', "Cameroon",
'Canada', "Canada",
'Cape Verde', "Cape Verde",
'Central African Republic', "Central African Republic",
'Chad', "Chad",
'Chile', "Chile",
'China', "China",
'Colombi', "Colombi",
'Comoros', "Comoros",
'Congo (Brazzaville)', "Congo (Brazzaville)",
'Congo', "Congo",
'Costa Rica', "Costa Rica",
"Cote d'Ivoire", "Cote d'Ivoire",
'Croatia', "Croatia",
'Cuba', "Cuba",
'Cyprus', "Cyprus",
'Czech Republic', "Czech Republic",
'Denmark', "Denmark",
'Djibouti', "Djibouti",
'Dominica', "Dominica",
'Dominican Republic', "Dominican Republic",
'East Timor (Timor Timur)', "East Timor (Timor Timur)",
'Ecuador', "Ecuador",
'Egypt', "Egypt",
'El Salvador', "El Salvador",
'Equatorial Guinea', "Equatorial Guinea",
'Eritrea', "Eritrea",
'Estonia', "Estonia",
'Ethiopia', "Ethiopia",
'Fiji', "Fiji",
'Finland', "Finland",
'France', "France",
'Gabon', "Gabon",
'Gambia, The', "Gambia, The",
'Georgia', "Georgia",
'Germany', "Germany",
'Ghana', "Ghana",
'Greece', "Greece",
'Grenada', "Grenada",
'Guatemala', "Guatemala",
'Guinea', "Guinea",
'Guinea-Bissau', "Guinea-Bissau",
'Guyana', "Guyana",
'Haiti', "Haiti",
'Honduras', "Honduras",
'Hungary', "Hungary",
'Iceland', "Iceland",
'India', "India",
'Indonesia', "Indonesia",
'Iran', "Iran",
'Iraq', "Iraq",
'Ireland', "Ireland",
'Israel', "Israel",
'Italy', "Italy",
'Jamaica', "Jamaica",
'Japan', "Japan",
'Jordan', "Jordan",
'Kazakhstan', "Kazakhstan",
'Kenya', "Kenya",
'Kiribati', "Kiribati",
'Korea, North', "Korea, North",
'Korea, South', "Korea, South",
'Kuwait', "Kuwait",
'Kyrgyzstan', "Kyrgyzstan",
'Laos', "Laos",
'Latvia', "Latvia",
'Lebanon', "Lebanon",
'Lesotho', "Lesotho",
'Liberia', "Liberia",
'Libya', "Libya",
'Liechtenstein', "Liechtenstein",
'Lithuania', "Lithuania",
'Luxembourg', "Luxembourg",
'Macedonia', "Macedonia",
'Madagascar', "Madagascar",
'Malawi', "Malawi",
'Malaysia', "Malaysia",
'Maldives', "Maldives",
'Mali', "Mali",
'Malta', "Malta",
'Marshall Islands', "Marshall Islands",
'Mauritania', "Mauritania",
'Mauritius', "Mauritius",
'Mexico', "Mexico",
'Micronesia', "Micronesia",
'Moldova', "Moldova",
'Monaco', "Monaco",
'Mongolia', "Mongolia",
'Morocco', "Morocco",
'Mozambique', "Mozambique",
'Myanmar', "Myanmar",
'Namibia', "Namibia",
'Nauru', "Nauru",
'Nepal', "Nepal",
'Netherlands', "Netherlands",
'New Zealand', "New Zealand",
'Nicaragua', "Nicaragua",
'Niger', "Niger",
'Nigeria', "Nigeria",
'Norway', "Norway",
'Oman', "Oman",
'Pakistan', "Pakistan",
'Palau', "Palau",
'Panama', "Panama",
'Papua New Guinea', "Papua New Guinea",
'Paraguay', "Paraguay",
'Peru', "Peru",
'Philippines', "Philippines",
'Poland', "Poland",
'Portugal', "Portugal",
'Qatar', "Qatar",
'Romania', "Romania",
'Russia', "Russia",
'Rwanda', "Rwanda",
'Saint Kitts and Nevis', "Saint Kitts and Nevis",
'Saint Lucia', "Saint Lucia",
'Saint Vincent', "Saint Vincent",
'Samoa', "Samoa",
'San Marino', "San Marino",
'Sao Tome and Principe', "Sao Tome and Principe",
'Saudi Arabia', "Saudi Arabia",
'Senegal', "Senegal",
'Serbia and Montenegro', "Serbia and Montenegro",
'Seychelles', "Seychelles",
'Sierra Leone', "Sierra Leone",
'Singapore', "Singapore",
'Slovakia', "Slovakia",
'Slovenia', "Slovenia",
'Solomon Islands', "Solomon Islands",
'Somalia', "Somalia",
'South Africa', "South Africa",
'Spain', "Spain",
'Sri Lanka', "Sri Lanka",
'Sudan', "Sudan",
'Suriname', "Suriname",
'Swaziland', "Swaziland",
'Sweden', "Sweden",
'Switzerland', "Switzerland",
'Syria', "Syria",
'Taiwan', "Taiwan",
'Tajikistan', "Tajikistan",
'Tanzania', "Tanzania",
'Thailand', "Thailand",
'Togo', "Togo",
'Tonga', "Tonga",
'Trinidad and Tobago', "Trinidad and Tobago",
'Tunisia', "Tunisia",
'Turkey', "Turkey",
'Turkmenistan', "Turkmenistan",
'Tuvalu', "Tuvalu",
'Uganda', "Uganda",
'Ukraine', "Ukraine",
'United Arab Emirates', "United Arab Emirates",
'United Kingdom', "United Kingdom",
'United States', "United States",
'Uruguay', "Uruguay",
'Uzbekistan', "Uzbekistan",
'Vanuatu', "Vanuatu",
'Vatican City', "Vatican City",
'Venezuela', "Venezuela",
'Vietnam', "Vietnam",
'Yemen', "Yemen",
'Zambia', "Zambia",
'Zimbabwe' "Zimbabwe",
] ];

@ -35,8 +35,8 @@ const itCompanies = [
"Apple", "Apple",
"IBM", "IBM",
"Oracle", "Oracle",
"Amazon", "Amazon",
"Netflix" "Netflix",
]; ];
//Print the array using console.log() //Print the array using console.log()
@ -74,7 +74,7 @@ let companySearch = "IBAM";
// console.log("Companies with double o") // console.log("Companies with double o")
let doubleo = []; let doubleo = [];
for (let company of itCompanies) { for (let company of itCompanies) {
company.match(/oo/gm) ? doubleo.push(company) : null company.match(/oo/gm) ? doubleo.push(company) : null;
} }
// console.log(doubleo); // console.log(doubleo);
@ -135,45 +135,116 @@ let words = text.replace(/\,|\./gm, "").split(" ");
// console.log(`Text Length: ${words.length}`); // console.log(`Text Length: ${words.length}`);
//In the following shopping cart add, remove, edit items //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 //Add 'Meat' in the beginning of your shoppinh cart if it has not been already added
let foodToAdd = "Meat"; let foodToAdd = "Meat";
let checkFood = shoppingCart.includes(foodToAdd); let checkFood = shoppingCart.includes(foodToAdd);
!checkFood ? shoppingCart.unshift(foodToAdd) : ''; !checkFood ? shoppingCart.unshift(foodToAdd) : "";
console.log(shoppingCart); // console.log(shoppingCart);
//Add Sugar at the end of your shopping cart if it has not already been added //Add Sugar at the end of your shopping cart if it has not already been added
let foodToAdd2 = "Sugar"; let foodToAdd2 = "Sugar";
let checkFood2 = shoppingCart.includes(foodToAdd2); let checkFood2 = shoppingCart.includes(foodToAdd2);
!checkFood2 ? shoppingCart.push(foodToAdd2) : ""; !checkFood2 ? shoppingCart.push(foodToAdd2) : "";
console.log(shoppingCart); // console.log(shoppingCart);
//Remove Honey if you are allergic to honey //Remove Honey if you are allergic to honey
let foodToRemove = "Honey" let foodToRemove = "Honey";
let allergy = shoppingCart.includes(foodToRemove); let allergy = shoppingCart.includes(foodToRemove);
allergy ? shoppingCart.splice(shoppingCart.indexOf(foodToRemove), 1) : null; allergy ? shoppingCart.splice(shoppingCart.indexOf(foodToRemove), 1) : null;
console.log(shoppingCart); // console.log(shoppingCart);
//Modify Tea to 'Green Tea' //Modify Tea to 'Green Tea'
let searchItem = "Tea"; let searchItem = "Tea";
let checkItem = shoppingCart.includes(searchItem); let checkItem = shoppingCart.includes(searchItem);
checkItem ? shoppingCart[shoppingCart.indexOf(searchItem)] = "Green Tea" : null; checkItem
console.log(shoppingCart); ? (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 //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"); 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 //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"); let checkWebTechs = webTechs.includes("Sass");
checkWebTechs ? console.log("Sass is a CSS Preprocessor") : webTechs.push("Sass"); checkWebTechs
console.log(webTechs); ? 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 //Concatenate the following two variables and store it in a fullStack variable
const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux']; const frontEnd = ["HTML", "CSS", "JS", "React", "Redux"];
const backend = ['Node', 'Express', 'MongoDB']; const backend = ["Node", "Express", "MongoDB"];
let fullStack = frontEnd.concat(backend); let fullStack = frontEnd.concat(backend);
console.log(fullStack); // 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);

Loading…
Cancel
Save