From f727fbcc487de759d46e1d807624d61f1eeba9db Mon Sep 17 00:00:00 2001 From: "belaid.daliomar@gmail.com" <111222029+belaid-dali@users.noreply.github.com> Date: Mon, 28 Aug 2023 10:49:30 +0100 Subject: [PATCH] I have completed the day 05 so far, rich content, loved it --- .../05_day_starter/data/countries.js | 41 ++++ 05_Day_Arrays/05_day_starter/data/webtechs.js | 19 ++ 05_Day_Arrays/05_day_starter/scripts/main.js | 187 +++++++++++++++++- 3 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 05_Day_Arrays/05_day_starter/data/webtechs.js diff --git a/05_Day_Arrays/05_day_starter/data/countries.js b/05_Day_Arrays/05_day_starter/data/countries.js index e57b005..83e55bd 100644 --- a/05_Day_Arrays/05_day_starter/data/countries.js +++ b/05_Day_Arrays/05_day_starter/data/countries.js @@ -193,3 +193,44 @@ const countries = [ 'Zambia', 'Zimbabwe' ] + + +const isEthiopia = countries.includes('Ethopia'); + +if(isEthiopia){ + console.log('Ethiopia'); +} else { + countries.push('Ethiopia'); + console.log(countries); + } + + + const firstTenCountries = countries.slice(0, 10); + console.log(firstTenCountries); + //['Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola', 'Antigua and Barbuda', 'Argentina', 'Armenia', 'Australia', 'Austria'] + + const middleIndex = Math.floor(countries.length / 2); + + if (countries.length % 2 === 1) { + // If the array has an odd number of countries + const middleCountry = countries[middleIndex]; + console.log('Middle Country (Odd):', middleCountry); + } else { + // If the array has an even number of countries + const middleCountry1 = countries[middleIndex - 1]; + const middleCountry2 = countries[middleIndex]; + console.log('Middle Countries (Even):', middleCountry1, middleCountry2); + } + + let firstHalf; + let secondHalf; + + if(countries.length % 2 === 0) { + firstHalf = countries.slice(0, middleIndex); + secondHalf = countries.slice(middleIndex); + } else { + firstHalf = countries.slice(0, middleIndex+1); + secondHalf = countries.slice(middleIndex + 1); + } + console.log('First Half:', firstHalf); + console.log('Second Half:', secondHalf); \ No newline at end of file diff --git a/05_Day_Arrays/05_day_starter/data/webtechs.js b/05_Day_Arrays/05_day_starter/data/webtechs.js new file mode 100644 index 0000000..e1098f1 --- /dev/null +++ b/05_Day_Arrays/05_day_starter/data/webtechs.js @@ -0,0 +1,19 @@ +const webTechs = [ + 'HTML', 'CSS', 'JavaScript', 'React', 'Node', + 'PHP', 'EastJS', 'SQL', 'SASS', 'AngolaJS' +] + +const isSass = webTechs.includes('SASS'); +if (isSass) { + console.log('SASS is a CSS preprocessor'); +} else { + webTechs.push('SASS'); + console.log(webTechs); +} + +//exo 6 +const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux'] +const backEnd = ['Node','Express', 'MongoDB'] +const fullStack = frontEnd.concat(backEnd); + +console.log(fullStack); \ No newline at end of file diff --git a/05_Day_Arrays/05_day_starter/scripts/main.js b/05_Day_Arrays/05_day_starter/scripts/main.js index 50cc07e..4c7e946 100644 --- a/05_Day_Arrays/05_day_starter/scripts/main.js +++ b/05_Day_Arrays/05_day_starter/scripts/main.js @@ -1,3 +1,188 @@ 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 +alert('Open the console and check if the countries has been loaded') + +//Exo level 01 + +const countries = [ + 'Albania', + 'Bolivia', + 'Canada', + 'Denmark', + 'Ethiopia', + 'Finland', + 'Germany', + 'Hungary', + 'Ireland', + 'Japan', + 'Kenya' + ] + + const webTechs = [ + 'HTML', + 'CSS', + 'JavaScript', + 'React', + 'Redux', + 'Node', + 'MongoDB' + ] + + //exo 01 + + const arr = new Array(); + console.log(arr); + + //exo 02 + + const arr2 = [1,1,2,3,4,5,6,7,8,9,10,11]; + console.log(arr2.length);// 12 + + //exo 04 + + console.log(arr2[0]); //1 + console.log(arr2[5]); //6 + console.log(arr2[arr2.length - 1 ]); //11 + + //exo 05 + const mixwsDataTypes = [1, 'amount', 'q',34, true]; + + //exo 06 + let itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon']; + console.log(itCompanies.length); +//(6) ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'] + console.log(itCompanies.length);//7 + console.log(itCompanies[0]);//Facebook + const middleIndex = Math.floor(itCompanies.length/2); + console.log(itCompanies[middleIndex]);//Apple + console.log(itCompanies[itCompanies.length - 1]);//Amazon + console.log(itCompanies[0]); + console.log(itCompanies[0]); + console.log(itCompanies[1]); + console.log(itCompanies[2]); + console.log(itCompanies[3]); + console.log(itCompanies[4]); + console.log(itCompanies[5]); + console.log(itCompanies[6]); + + console.log(itCompanies[0].toUpperCase()); + console.log(itCompanies[1].toUpperCase()); + console.log(itCompanies[2].toUpperCase()); + console.log(itCompanies[3].toUpperCase()); + console.log(itCompanies[4].toUpperCase()); + console.log(itCompanies[5].toUpperCase()); + console.log(itCompanies[6].toUpperCase()); + + console.log(itCompanies.join()); + + // + const companyToFind = prompt('Enter a company:'); + const companyExists = itCompanies.includes(companyToFind); + + companyExists ? console.log(`${companyToFind} already exists`) : console.log(`${companyToFind} not found`); + + // + const itCompanies = ["Facebook", "Google", "Microsoft", "Apple", "IBM", "Oracle", "Amazon"]; + +// Iterate through each company and count the 'o' occurrences +console.log("Companies with more than one 'o':"); +if (itCompanies[0].toLowerCase().split('o').length - 1 > 1) { + console.log(itCompanies[0]); +} +if (itCompanies[1].toLowerCase().split('o').length - 1 > 1) { + console.log(itCompanies[1]); +} +if (itCompanies[2].toLowerCase().split('o').length - 1 > 1) { + console.log(itCompanies[2]); +} +if (itCompanies[3].toLowerCase().split('o').length - 1 > 1) { + console.log(itCompanies[3]); +} +if (itCompanies[4].toLowerCase().split('o').length - 1 > 1) { + console.log(itCompanies[4]); +} +if (itCompanies[5].toLowerCase().split('o').length - 1 > 1) { + console.log(itCompanies[5]); +} +if (itCompanies[6].toLowerCase().split('o').length - 1 > 1) { + console.log(itCompanies[6]); +} + +// +itCompanies.sort(); +itCompanies.reverse(); +itCompanies.slice(0, 3); +itCompanies.slice(-3);//['IBM', 'Oracle', 'Amazon'] +itCompanies.shift();//'Facebook' + +const itCompanies = ["Facebook", "Google", "Microsoft", "Apple", "IBM", "Oracle", "Amazon"]; + +const middleIndex = Math.floor(itCompanies.length / 2); +const numberOfCompaniesToSlice = itCompanies.length % 2 === 0 ? 2 : 1; // 1 for odd length, 2 for even length + +const middleCompanies = itCompanies.slice(middleIndex, middleIndex + numberOfCompaniesToSlice); +console.log("Middle company(s):", middleCompanies); + +itCompanies.splice(middleCompanies, middleIndex+numberOfCompaniesToSlice); + +itCompanies.pop(); + +itCompanies.splice();//[] + +//exercice level 02 + +//exo 01 +let text = 'I love teaching and empowering people. I teach HTML, CSS, JS, React, Python.' +const sanitizedText = text.replace(/[.,]/g, ''); +const wordsArray = sanitizedText.split(' '); +console.log(wordsArray); +console.log(wordsArray.length); + +//exo 02 + +const shoppingCart = ['Milk', 'coffee', 'tea', 'Honey']; +shoppingCart.unshift('meat'); +console.log(shoppingCart);//['meat','Milk', 'coffee', 'tea', 'Honey'] +shoppingCart.push('sugar');//['meat','Milk', 'coffee', 'tea', 'Honey', 'sugar'] +shoppingCart.splice(4, 1); +console.log(shoppingCart);// ['meat', 'Milk', 'coffee', 'tea', 'sugar'] +shoppingCart[3]='green tea'; +console.log(shoppingCart);//['meat', 'Milk', 'coffee', 'green tea', 'sugar'] + +//exercice level 03 + +//exo 01 + +const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24]; +const arrangedArray = ages.sort(); +console.log(arrangedArray); +const min = arrangedArray[0]; +const max = arrangedArray[ages.length - 1]; + +const medianAge = Math.floor(arrangedArray.length / 2); + +if(ages.length % 2 == 0) { + //if the array has an even number of values + const medianVal1 = arrangedArray[medianAge -1]; + const medianVal2 = arrangedArray[medianAge]; + const median = (medianVal1 + medianVal2)/2; + console.log('median age is:', median); +} else { + //if the array has an odd number of values + const median = arrangedArray[medianAge]; +}//24 + +const sumOfAges = ages.reduce((sum, age) => sum + age, 0); +const averageAge = sumOfAges / ages.length; +console.log('average age is:', averageAge);//22.8 + +const minAge = Math.min(...ages); +const maxAge = Math.max(...ages); + +const ageRange = maxAge - minAge; +console.log('min age range is:', minAge, 'max age range is:', maxAge, 'age range is:', ageRange); + +const minDiff = Math.abs(minAge - averageAge); +const maxDiff = Math.abs(maxAge - averageAge); + +console.log('min diff is:', minDiff, 'max diff is:',maxDiff); \ No newline at end of file