diff --git a/02_Day_Data_types/02_day_starter/main.js b/02_Day_Data_types/02_day_starter/main.js index 7762908..b18779f 100644 --- a/02_Day_Data_types/02_day_starter/main.js +++ b/02_Day_Data_types/02_day_starter/main.js @@ -1 +1,155 @@ -// this is your main.js script \ No newline at end of file +// this is your main.js script +// Exercise 01 + +// Declare a variable named challenge and assign it an initial value +let challenge = '30 Days Of JavaScript'; + +// Print the string on the browser console +console.log(challenge); + +// Print the length of the string +console.log(challenge.length); + +// Change all characters to capital letters +console.log(challenge.toUpperCase()); + +// Change all characters to lowercase letters +console.log(challenge.toLowerCase()); + +// Slice out the first word of the string +console.log(challenge.substr(0, 2)); // Assuming you want the first word "30" + +// Slice out the phrase "Days Of JavaScript" +console.log(challenge.substring(3)); // Assuming you want to exclude the initial "30 " + +// Check if the string contains the word "Script" +console.log(challenge.includes('Script')); + +// Split the string into an array +console.log(challenge.split()); + +// Split the string at the space +console.log(challenge.split(' ')); + +// Split the string at the comma and change it to an array +let companies = 'Facebook, Google, Microsoft, Apple, IBM, Oracle, Amazon'; +console.log(companies.split(', ')); + +// Change "30 Days Of JavaScript" to "30 Days Of Python" +console.log(challenge.replace('JavaScript', 'Python')); + +// Character at index 15 +console.log(challenge.charAt(15)); + +// Character code of J +console.log(challenge.charCodeAt(challenge.indexOf('J'))); + +// Position of first occurrence of 'a' +console.log(challenge.indexOf('a')); + +// Position of last occurrence of 'a' +console.log(challenge.lastIndexOf('a')); + +// Position of first occurrence of 'because' in a sentence +let sentence = 'You cannot end a sentence with because because because is a conjunction'; +console.log(sentence.indexOf('because')); + +// Position of last occurrence of 'because' in a sentence +console.log(sentence.lastIndexOf('because')); + +// Position of first occurrence of 'because' using search +console.log(sentence.search('because')); + +// Remove trailing whitespace +console.log(challenge.trim()); + +// Check if it starts with '30 Days Of JavaScript' +console.log(challenge.startsWith('30 Days Of JavaScript')); + +// Check if it ends with '30 Days Of JavaScript' +console.log(challenge.endsWith('30 Days Of JavaScript')); + +// Find all occurrences of 'a' using match +console.log(challenge.match(/a/g)); + +// Concatenate strings +console.log('30 Days of '.concat('JavaScript')); + +// Repeat the string +console.log(challenge.repeat(2)); + +//Excercise JavaScript level 02 + +// Exercise 1 +console.log("The quote 'There is no exercise better for the heart than reaching down and lifting people up.' by John Holmes teaches us to help one another."); + +// Exercise 2 +console.log("\"Love is not patronizing and charity isn't about pity, it is about love. Charity and love are the same -- with charity you give love, so don't just give money but reach out your hand instead.\""); + +// Exercise 3 +if (typeof '10' !== 'number') { + '10' = parseInt('10'); +} + +// Exercise 4 +if (parseFloat('9.8') !== 10) { + '9.8' = parseFloat('10'); +} + +// Exercise 5 +const python = 'python'; +const jargon = 'jargon'; +console.log('on' in python && 'on' in jargon); + +// Exercise 6 +const sentence = 'I hope this course is not full of jargon.'; +console.log(sentence.includes('jargon')); + +// Exercise 7 +const randomNumber1 = Math.floor(Math.random() * 101); +console.log(randomNumber1); + +// Exercise 8 +const randomNumber2 = Math.floor(Math.random() * 51) + 50; +console.log(randomNumber2); + +// Exercise 9 +const randomNumber3 = Math.floor(Math.random() * 256); +console.log(randomNumber3); + +// Exercise 10 +const jsString = 'JavaScript'; +const randomIndex = Math.floor(Math.random() * jsString.length); +console.log(jsString[randomIndex]); + +// Exercise 11 +console.log("1 1 1 1 1"); +console.log("2 1 2 4 8"); +console.log("3 1 3 9 27"); +console.log("4 1 4 16 64"); +console.log("5 1 5 25 125"); + +// Exercise 12 +const sentence2 = 'You cannot end a sentence with because because because is a conjunction'; +const extractedPhrase = sentence2.substr(30, 23); +console.log(extractedPhrase); + +//Exercise level 03 + +const sentence1 = 'Love is the best thing in this world. Some found their love and some are still looking for their love.'; +const countLove1 = (sentence1.match(/love/gi) || []).length; +console.log(countLove1); // Output: 3 + +// +const sentence2 = 'You cannot end a sentence with because because because is a conjunction'; +const countBecause = (sentence2.match(/\bbecause\b/gi) || []).length; +console.log(countBecause); // Output: 3 + +// +const text4 = 'He earns 5000 euro from salary per month, 10000 euro annual bonus, 15000 euro online courses per month.'; + +// Extract numbers using regular expression and sum them up +const numbers = text4.match(/\d+/g).map(Number); +const totalAnnualIncome = numbers.reduce((total, num) => total + num, 0); + +console.log(totalAnnualIncome); // Output: 30000 diff --git a/03_Day_Booleans_operators_date/03_day_starter/scripts/main.js b/03_Day_Booleans_operators_date/03_day_starter/scripts/main.js index 7762908..f5952f0 100644 --- a/03_Day_Booleans_operators_date/03_day_starter/scripts/main.js +++ b/03_Day_Booleans_operators_date/03_day_starter/scripts/main.js @@ -1 +1,280 @@ -// this is your main.js script \ No newline at end of file +// this is your main.js script + +//exercice level 1 +//ex 01 +let firstName = 'Belaid'; +let lastName = 'DALI OMAR'; +let country = 'Algeria'; +let city = 'Tizi Ouzou'; +let age = 30; +let isMarried = false; +let year = 1993; + +console.log(typeof(firstName)); //string + +console.log(typeof(lastName)); //string + +console.log(typeof(country)); //string + +console.log(typeof(city)); //string + +console.log(typeof(age)); //number + +console.log(typeof(isMarried)); //boolean + +console.log(typeof(year)); //number + +//exo 02 +console.log('10' == 10); //true + +//exo 03 +let l = parseInt('9.8'); +console.log(a == 10); //False + +//exo 04 +//Truthy values: + +let truthyValue = 42; +let truthyString = "Hello, world!"; +let truthyArray = [1, 2, 3]; + +// Falsy values: + +let falsyValue = 0; +let falsyString = ""; +let falsyNull = null; + +//exo 05 +console.log(4>3); //true +console.log(4>=3); //true +console.log(4<3); //false +console.log(4 == 4); //true +console.log(4 === 4); //true +console.log(4 != 4); //False +console.log(4 !== 4); //false +console.log(4 != '4'); //false +console.log(4 == '4'); //true +console.log(4 === '4'); //false + +const pythonLength = "python".length; +const jargonLength = "jargon".length; + +const isFalsyComparison = pythonLength === jargonLength ? "Falsy" : "Not Falsy"; + +console.log(isFalsyComparison); + +//exo 06 + +console.log(4 > 3 && 10 < 12); //true +console.log(4 > 3 && 10 > 12); //false +console.log(4 > 3 || 10 < 12); //true +console.log(4 > 3 || 10 > 12); //true +console.log(!(4 > 3)); //false +console.log(!(4 < 3)); //true +console.log(!(false)); //true +console.log(!(4 > 3 && 10 < 12)); //false +console.log(!(4 > 3 && 10 > 12)); //true +console.log(!(4 === '4')); //true + +const str1 = 'python'; +const str2 = 'dragon'; +const isFalsyVal = str1.includes('on') || str2.includes('on') ? true : false; +console.log(isFalsyVal); //true + +//exo 07 + +const time = new Date(); +console.log(time); //Wed Aug 23 2023 10:39:33 GMT+0100 (Central European Standard Time) +console.log(time.getFullYear()); // 2023 +console.log(time.getMonth() + 1); // 7 +console.log(time.getDate()); // 23 +console.log(time.getDay()); // 3 +console.log(time.getHours()); // 10 +console.log(time.getMinutes()); // 39 +console.log(time.getTime()); // 1692783573531 seconds since January 1, 1970 00:00:00 GMT to 10:39:33 GMT + +//Exercice level 02 + +//exo 01 +const base = parseFloat(prompt('Enter base:')); +const h = parseFloat(prompt('Enter Height:')); +let area = 0.5*base*h; +console.log(`the area is ${area}`); // 100 + +// exo 02 + +let a = parseFloat(prompt('Enter side a:')); +let b = parseFloat(prompt('Enter side b:')); +let c = parseFloat(prompt('Enter side c:')); +let p = a+b+c; +console.log(`the perimeter is ${p}`); //12 + +//exo 03 + +let length = parseFloat(prompt('Enter L: ')); +let width = parseFloat(prompt('Enter W: ')); +let areas = length * width; +let perimeter = 2*(length+width); +console.log(`the perimeter is ${perimeter}`); // +console.log(`the area is ${areas}`); // + +//exo 04 + +let r = parseFloat(prompt('Enter L: ')); +const Pi = 3.14; +let ar = r * r * Pi; +let per= 2*Pi*r; +console.log(`the perimeter is ${per}`); // +console.log(`the area is ${ar}`); // + +//ex 05 + +// Equation: y = 2x - 2 + +// Slope (m) is the coefficient of x +const slope = 2; + +// x-intercept: When y = 0 +// 0 = 2x - 2 +// 2x = 2 +// x = 1 +const xIntercept = 1; + +// y-intercept: When x = 0 +const yIntercept = -2; + +console.log(`Slope (m): ${slope}`); +console.log(`x-intercept: (${xIntercept}, 0)`);//(1,0) +console.log(`y-intercept: (0, ${yIntercept})`);//(0,-2) + +//EXO 06 + +// Points +const x1 = 2; +const y1 = 2; +const x2 = 6; +const y2 = 10; + +// Calculate the slope +const slopes = (y2 - y1) / (x2 - x1); + +console.log(`The slope between (${x1}, ${y1}) and (${x2}, ${y2}) is ${slopes}`);//2 + +//exo 07 + +// Equation: y = 2x - 2 + +// Given points +const x1 = 2; +const y1 = 2; +const x2 = 6; +const y2 = 10; + +// Calculate the slope for the equation y = 2x - 2 +const equationSlope = 2; + +// Calculate the slope between the two points +const pointSlope = (y2 - y1) / (x2 - x1); + +// Compare the slopes using ternary operator +const comparisonResult = equationSlope === pointSlope ? "Equal" : "Not Equal"; + +console.log(`Slope of y = 2x - 2: ${equationSlope}`);//Slope of y = 2x -2: 2 +console.log(`Slope between (${x1}, ${y1}) and (${x2}, ${y2}): ${pointSlope}`);//slope between (2,2) and (6,10): 2 +console.log(`Comparison result: Slopes are ${comparisonResult}`);// comparison result: Slopes are Equal + +//Exo 08 + +// Equation: y = x^2 + 6x + 9 + +// Given equation coefficients +const a = 1; +const b = 6; +const c = 9; + +// Calculate the discriminant +const discriminant = Math.sqrt(b * b - 4 * a * c); + +// Calculate the two possible x values when y is 0 +const x1 = (-b + discriminant) / (2 * a); +const x2 = (-b - discriminant) / (2 * a); + +console.log(`x1: ${x1}`);//-3 +console.log(`x2: ${x2}`);//-3 + +//exo 09 + +let hours = parseFloat(prompt('Enter Hour')); +let rateHr = parseFloat(prompt('Enter rate per hour')); +const salary = hours * rateHr; +console.log(`Your weekly earning is ${salary}`);//Your weekly earning is 1120 + +//exo 10 + +const name = 'Belaid'; +let L = name.length; +(L > 7) +? console.log('my name is long') +: console.log('my name is short') +//my name is short + +//exo 11 + +let firstName = 'Belaid'; +let lastName = 'DALI OMAR'; +(length.firstName > length.lastName) +? console.log(`my first name, ${firstName} is longer than my family name, ${lastName}`) +: console.log(`my last name, ${lastName} is longer than my first name, ${firstName}`) +//my last name, DALI OMAR is longer than my first name, Belaid + +//exo 12 + +let myAge = 30; +let yourAge = 32; +console.log(`you are ${yourAge - myAge} years older than me`); +// you are 2 years older than me + +//exo 13 + +let birthYear = parseInt(prompt('Enter your birth year')); +let ages = 2023 - birthYear; +(ages>18) +? console.log(`you are ${ages}. You are old enough to drive`) +: console.log(`you are ${ages}. You will not be allowed to drive`); +//you are 29. You are old enough to drive + +//exo 14 + +let yrs = parseInt(prompt('Enter number of years you live')); +const live = new Date(); +console.log(`you lived ${live.getTime()} seconds`); +//you lived 1692797539157 seconds + +//exo 15 + +const now = new Date(); +const years = now.getFullYear(); // return year +const month = now.getMonth() + 1; // return month(0 - 11) +const date = now.getDate(); // return date (1 - 31) +const Hours = now.getHours(); // return number (0 - 23) +const minutes = now.getMinutes(); // return number (0 -59) + +console.log(`${years}-${month}-${date} ${Hours}:${minutes}`); +//2023-8-23 14:38 +console.log(`${date}-${month}-${years} ${Hours}:${minutes}`); +//23-8-2023 14:38 +console.log(`${date}/${month}/${years} ${Hours}:${minutes}`); +// 23/8/2023 14:38 + +//exercise level 3 + +//exo 01 + + +const months = String(date.getMonth() + 1).padStart(2, '0'); +const day = String(date.getDate()).padStart(2, '0'); +const hourss = String(date.getHours()).padStart(2, '0'); +const Minutes = String(date.getMinutes()).padStart(2, '0'); + + +console.log(`${years}-${months}-${day} ${hourss}:${Minutes}`); \ No newline at end of file diff --git a/04_Day_Conditionals/04_day_starter/scripts/main.js b/04_Day_Conditionals/04_day_starter/scripts/main.js index ad0c1eb..e298514 100644 --- a/04_Day_Conditionals/04_day_starter/scripts/main.js +++ b/04_Day_Conditionals/04_day_starter/scripts/main.js @@ -1,3 +1,139 @@ // this is your main.js script -alert('Open the browser console whenever you work on JavaScript') \ No newline at end of file +alert('Open the browser console whenever you work on JavaScript') + +//Exo level 1 + +//exo 01 +let age = prompt('Enter your age'); +if(age>=18){ + "you are old enough to drive"; +} else { + `you are left with ${18-age} years to drive`; +} + +//exo 02 + +let myAge = prompt('Enter your age'); +const yourAge = 45; +if(myAge>yourAge){ + `Um ${myAge-yourAge} years oder than you`; +} else { + `you are ${yourAge-myAge} years older than me`; +} + +//exo 03 + +let a = parseInt(prompt('Enter a number')); +let b= parseInt(prompt('Enter another number')); +(a>b) ? console.log('a is greater than b') : console.log('b is greater than a'); + +//exo 04 + +let c = parseInt(prompt('Enter a number')); +if(c%2==0){ + console.log(`${c} is even`); +} else { + console.log(`${c} is odd`); +} + +//exo level 2 + +let score = parseInt(prompt('Give your score:')); + +let grade; + +switch (true) { + case (score >= 80 && score <= 100): + grade = "A"; + break; + case (score >= 70 && score < 80): + grade = "B"; + break; + case (score >= 60 && score < 70): + grade = "C"; + break; + case (score >= 50 && score < 60): + grade = "D"; + break; + case (score >= 0 && score < 50): + grade = "F"; + break; + default: + grade = "Failed"; +} + +console.log(`Your grade is ${grade}`); + +//exo 02 + +let monthInput = prompt('Enter a month:'); +let month = monthInput.toLowerCase(); +let season; +switch (true) { + case month=="september" || month=="october" || month=="november" : + season = "Autumn"; + break; + case month=="decembre" || month=="january" || month=="february" : + season = "Winter"; + break; + case month=="march" || month=="april" || month=="may" : + season = "Spring"; + break; + case month=="june" || month=="july" || month=="august": + season = "Summer"; + break; + default : + season = ""; +} + +//exo 03 + +let dayInput = prompt("What is the day today?"); +let day = dayInput.toLowerCase(); + +if (day === "saturday" || day === "sunday") { + console.log(`${day} is a weekend.`); +} else if (day === "monday" || day === "tuesday" || day === "wednesday" || day === "thursday" || day === "friday") { + console.log(`${day} is a working day.`); +} else { + console.log("Invalid day."); +} + +//exo level 3 + +//exo 01 + +let monthIn = prompt("Enter a month"); +let months = monthIn.toLowerCase(); +let days; + +switch (months) { + case "january": + case "march": + case "july": + case "august": + case "october": + case "december": + days = 31; + break; + case "april": + case "june": + case "september": + case "november": + days = 30; + break; + case "february": + let yearInput = parseInt(prompt("Enter a year: ")); + let isLeapYear = (yearInput % 4 === 0 && yearInput % 100 !== 0) || (yearInput % 4 === 0); + days = isLeapYear ? 29 : 28; + break; + default: + console.error("Invalid month"); + break; +} + +if (days !== undefined) { + +console.log(`${months} has ${days}`); +} \ 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.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 diff --git a/06_Day_Loops/06_day_starter/scripts/main.js b/06_Day_Loops/06_day_starter/scripts/main.js index c6045c8..7a85b96 100644 --- a/06_Day_Loops/06_day_starter/scripts/main.js +++ b/06_Day_Loops/06_day_starter/scripts/main.js @@ -1,2 +1,395 @@ console.log(countries) -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') + +//Exercice 01 + +const countries = [ + 'Albania', + 'Bolivia', + 'Canada', + 'Denmark', + 'Ethiopia', + 'Finland', + 'Germany', + 'Hungary', + 'Ireland', + 'Japan', + 'Kenya' + ] + + const webTechs = [ + 'HTML', + 'CSS', + 'JavaScript', + 'React', + 'Redux', + 'Node', + 'MongoDB' + ] + + const mernStack = ['MongoDB', 'Express', 'React', 'Node']; + + // Exo 01 + for(let i = 0; i < countries.length; i++) { + console.log(countries[i]); + } + //backforward + const words = countries.length; + for(let i = words; i >= 0; i--) { + console.log(countries[i]); + } + + //while loop + let i =0; + while(i < webTechs.length) { + console.log(webTechs[i]); + i++; + } + //backforwar + let i = webTechs.length; + while(i>=0){ + console.log(webTechs[i]); + i--; + } + + //do ... while loop + let x = 0; + do{ + console.log(mernStack[x]); + x++; + } while(x < mernStack.length); + + //backforward + let w = mernStack.length; + do{ + console.log(mernStack[w]); + w--; + }while(w >= 0); + + //exo 03 + let n = 17; + for(let i = 0; i 0) { + console.log(isLand); +} else { + console.log("All these countries are withoun 'land'"); +} + +//exo 08 +let isIA = []; +for(let i=0; i < countries.length; i++){ + if(countries[i].toLowerCase().endsWith('ia')){ + isIA.push(countries[i]); + } +} +if(isIA.length > 0) { + console.log(isIA); +} else { + console.log("These are countries ends without ia"); +} +// ['Albania', 'Bolivia', 'Ethiopia'] + +//exo 09 +let big = ''; +for(let i=0; i < countries.length; i++){ + if(countries[i].length> big.length){ + big = countries[i]; + } +} +console.log(big); + +//exo 10 +let isBig = []; +for(let i=0; i longest.length){ + longest = webTechs[i]; + } +} +console.log(longest);//JavaScript + +//exo 12 +let array1 = []; +for (let i = 0; i < webTechs.length; i++){ + let c = webTechs[i].length; + array1.push([webTechs[i], c]); +} +console.log(array1); + +//exo 13 +let accronym = ''; +for (let i = 0; i < mernStack.length; i++) { + accronym += mernStack[i].charAt(0); +} +console.log(accronym);//MERN + +//exo 14 +for(const i of webTechs){ + console.log(webTechs[i]; +} + +//exo 15 +let fruits = ['banana', 'orange', 'mango', 'lemon']; + +for (let i = fruits.length; i >= 0; i--) { + console.log(fruits[i]); +} + +//exo 16 +const fullStack = [ + ['HTML', 'CSS', 'JS', 'React'], + ['Node', 'Express', 'MongoDB'] +]; + +for (let i = 0; i < fullStack.length; i++){ + for (let j = 0; j < fullStack[i].length; j++){ + console.log(fullStack[i][j]) + } +} + +//exercise level 03 + +const countries = [ + 'Albania', 'Bolivia', 'Canada', 'Denmark', 'Ethiopia', 'Finland', + 'Germany', 'Hungary', 'Ireland', 'Japan', 'Kenya', 'Malaysia' +]; + +// Create a copy of the array without modifying the original +const copiedCountries = [...countries]; + +// Sort the copied array +const sortedCountries = copiedCountries.slice().sort(); + +// Sort the webTechs array +const webTechs = ['HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node', 'MongoDB']; +const sortedWebTechs = webTechs.slice().sort(); + +// Sort the mernStack array +const mernStack = ['MongoDB', 'Express', 'React', 'Node']; +const sortedMernStack = mernStack.slice().sort(); + +// Extract countries containing 'land' +const countriesWithLand = []; +for (let i = 0; i < countries.length; i++) { + if (countries[i].toLowerCase().includes('land')) { + countriesWithLand.push(countries[i]); + } +} + +// Find the country with the highest number of characters +let longestCountry = ''; +for (let i = 0; i < countries.length; i++) { + if (countries[i].length > longestCountry.length) { + longestCountry = countries[i]; + } +} + +// Extract countries containing only four characters +const countriesWithFourChars = []; +for (let i = 0; i < countries.length; i++) { + if (countries[i].length === 4) { + countriesWithFourChars.push(countries[i]); + } +} + +// Extract countries containing two or more words +const countriesWithMultipleWords = []; +for (let i = 0; i < countries.length; i++) { + if (countries[i].includes(' ')) { + countriesWithMultipleWords.push(countries[i]); + } +} + +// Reverse and capitalize countries array +const reversedAndCapitalizedCountries = []; +for (let i = countries.length - 1; i >= 0; i--) { + reversedAndCapitalizedCountries.push(countries[i].toUpperCase()); +} + +console.log('Sorted Countries:', sortedCountries); +console.log('Sorted Web Techs:', sortedWebTechs); +console.log('Sorted MERN Stack:', sortedMernStack); +console.log('Countries with "land":', countriesWithLand); +console.log('Country with the most characters:', longestCountry); +console.log('Countries with four characters:', countriesWithFourChars); +console.log('Countries with multiple words:', countriesWithMultipleWords); +console.log('Reversed and Capitalized Countries:', reversedAndCapitalizedCountries);