diff --git a/11_Day_Destructuring_and_spreading/11_day_starter/scripts/main.js b/11_Day_Destructuring_and_spreading/11_day_starter/scripts/main.js index 370b77f..1bd4d91 100644 --- a/11_Day_Destructuring_and_spreading/11_day_starter/scripts/main.js +++ b/11_Day_Destructuring_and_spreading/11_day_starter/scripts/main.js @@ -73,7 +73,8 @@ let [fin, est, sw, den, nor] = countries_ //1. Iterate through the users array and get all the keys of the object using destructuring for (const {name, scores, skills, age} of users) { - console.log(Object.keys({name, scores, skills, age})); + //console.log(Object.keys({name, scores, skills, age})); + console.log(skills, skills.length) } // get the keys without destructuring @@ -107,7 +108,7 @@ const students = [ const convertArrayToObject = (arr) => { for (const [name, skills, scores] of arr) { - console.log({name: name, skills: skills, scores: scores}); + return {name: name, skills: skills, scores: scores}; } } console.log(convertArrayToObject(students)); @@ -118,10 +119,12 @@ const students = [ //Add SQL with level 8 to the data base skill sets //Add SQL without level to the data science skill sets -const newStudent = {...student} +/*const newStudent = {...student} const {name, age, skills} = newStudent; let {frontEnd, backEnd, dataBase, dataScience} = skills; frontEnd.push(skills, {skill: 'Bootstrap', level: 8}); backEnd.push({skill: 'Express', level: 9}); dataBase.push({skill: 'SQL', level: 8}); -dataScience.push('SQL'); \ No newline at end of file +dataScience.push('SQL');*/ + + diff --git a/12_Day_Regular_expressions/12_day_starter/scripts/main.js b/12_Day_Regular_expressions/12_day_starter/scripts/main.js index c6045c8..6dd8b41 100644 --- a/12_Day_Regular_expressions/12_day_starter/scripts/main.js +++ b/12_Day_Regular_expressions/12_day_starter/scripts/main.js @@ -1,2 +1,134 @@ + 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') + +//Exercises + +//Exercises: Level 1 + +//1. Calculate the total annual income of the person from the following text. ‘He earns 4000 euro from salary per month, 10000 euro annual bonus, 5500 euro online courses per month.’ +let text = 'He earns 4000 euro from salary per month, 10000 euro annual bonus, 5500 euro online courses per month.' + +const pattern = /\d+/g +const matches = text.match(pattern); +console.log(matches) +let [salary, annualBonus, onlineCourses] = matches +console.log((+salary + + onlineCourses) * 12 + +annualBonus) // 124000 + + +//2. The position of some particles on the horizontal x-axis -12, -4, -3 and -1 in the negative direction, 0 at origin, 4 and 8 in the positive direction. Extract these numbers and find the distance between the two furthest particles. +const textParticles = 'The position of some particles on the horizontal x-axis -12, -4, -3 and -1 in the negative direction, 0 at origin, 4 and 8 in the positive direction.'; +const patternParticles = /.\d+/g; +const points = textParticles.match(patternParticles); +console.log(points) + +const sortedPoints = []; +for (let element of points) { + sortedPoints.push(parseInt(element)); +} +console.log(sortedPoints) + +let distance = sortedPoints[sortedPoints.length - 1] -sortedPoints[0]; +console.log(distance) + +//3. Write a pattern which identify if a string is a valid JavaScript variable +function isValidVariable(str) { + const pattern1 = /^\d/; + const pattern2 = /[^A-Za-z0-9$_]/; + bool1 = pattern1.test(str); + bool2 = pattern2.test(str); + if (bool1||bool2) { + return 'Invalid'; + } else { + return 'Valid'; + } + } + console.log(isValidVariable('first_name')) + + //Exercises: Level 2 + + //1. Write a function called tenMostFrequentWords which get the ten most frequent word from a string? + + paragraph = `I love teaching. If you do not love teaching what else can you love. I love Python if you do not love something which can give you all the capabilities to develop an application what else can you love.` + + /*[ + {word:'love', count:6}, + {word:'you', count:5}, + {word:'can', count:3}, + {word:'what', count:2}, + {word:'teaching', count:2}, + {word:'not', count:2}, + {word:'else', count:2}, + {word:'do', count:2}, + {word:'I', count:2}, + {word:'which', count:1}, + {word:'to', count:1}, + {word:'the', count:1}, + {word:'something', count:1}, + {word:'if', count:1}, + {word:'give', count:1}, + {word:'develop',count:1}, + {word:'capabilities',count:1}, + {word:'application', count:1}, + {word:'an',count:1}, + {word:'all',count:1}, + {word:'Python',count:1}, + {word:'If',count:1}] */ + + function tenMostFrequentWords(str, number) { + const pattern = /[^A-Za-z ]/g; + const onlyWords = str.replace(pattern, ''); + const wordIndex = [...new Set(onlyWords.split(' '))]; + const wordsAndCounts = []; + for (i = 0; i < wordIndex.length; i++) { + let word = new RegExp(wordIndex[i], 'g'); + wordsAndCounts.push({word: wordIndex[i], count: onlyWords.match(word).length}) + } + const sortedWordCounts = wordsAndCounts.sort((a, b) => + b.count - a.count + ); + const printOut = sortedWordCounts.filter((object) => + sortedWordCounts.indexOf(object) < number); + return printOut; + } + console.log(tenMostFrequentWords(paragraph, 10)); + + // Exercises: Level 3 + + //1. Write a function which cleans text. Clean the following text. After cleaning, count three most frequent words in the string. + + sentence = `%I $am@% a %tea@cher%, &and& I lo%#ve %tea@ching%;. There $is nothing; &as& mo@re rewarding as educa@ting &and& @emp%o@wering peo@ple. ;I found tea@ching m%o@re interesting tha@n any other %jo@bs. %Do@es thi%s mo@tivate yo@u to be a tea@cher!?` + //console.log(cleanText(sentence)) + +/*js + console.log(mostFrequentWords(cleanedText)) + [{word:'I', count:3}, {word:'teaching', count:2}, {word:'teacher', count:2}]*/ + + // cleaning the text +function cleanText(str) { + const pattern = /[^A-Za-z ]/g; + const cleanedText = str.replace(pattern, ''); + return cleanedText; + } + console.log(cleanText(sentence)); + + function mostFrequentWords(str) { + const cleanedText = cleanText(str); + const wordIndex = [...new Set(cleanedText.split(' '))]; + const wordsAndCounts = []; + for (i = 0; i < wordIndex.length; i++) { + let word = new RegExp(wordIndex[i],'g'); + console.log(word) + wordsAndCounts[i] = { word: wordIndex[i], count: cleanedText.match(word).length }; + } + const sortedWordCounts = wordsAndCounts.sort((a, b) => + b.count - a.count + ); + const printOut = sortedWordCounts.filter((object) => + sortedWordCounts.indexOf(object) < 3); + return printOut; + }; + console.log(mostFrequentWords(sentence)); + + + diff --git a/13_Day_Console_object_methods/13_day_starter/scripts/main.js b/13_Day_Console_object_methods/13_day_starter/scripts/main.js index c6045c8..3ba4674 100644 --- a/13_Day_Console_object_methods/13_day_starter/scripts/main.js +++ b/13_Day_Console_object_methods/13_day_starter/scripts/main.js @@ -1,2 +1,77 @@ 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') + +//Exercises + +//Exercises:Level 1 + +//1. Display the countries array as a table +const countries_ = [ + 'Albania', + 'Belgium', + 'Canada', + 'Denmark', + 'Estonia', + 'Finland' + ] + +console.table(countries_); +//2. Display the countries object as a table +const canada = { + "name": "Canada", + "capital": "Ottawa", + "population": 38005238, + "region": "Americas", + "area": 9984670 +} +console.table(canada) + +//3. Use console.group() to group logs +console.group('Group of countries'); +console.group(countries_); +console.groupEnd() + +//Exercises:Level 2 + +//1. 10 > 2 * 10 use console.assert() +console.assert(10 > 2 * 10, '10 is larger than 2 times 10.'); +//2. Write a warning message using console.warn() +console.warn('Stay focused!'); +//3. Write an error message using console.error() +console.error('3 errors detected'); +//Exercises:Level 3 + +//1. Check the speed difference among the following loops: while, for, for of, forEach +const numbers = [0, 1, 2, 3, 4]; + +// while loop +console.time('while loop'); +let i = 0 +do { + console.log(numbers[i]); + i++; +} while (i < 5); +console.timeEnd('while loop'); + + +// for loop +console.time('for loop'); +for (i = 0; i < 5; i++) { + console.log(numbers[i]); +} +console.timeEnd('for loop'); + + +// for of loop +console.time('for of loop'); +for (const element of numbers) { + console.log(element); +} +console.timeEnd('for of loop'); + + +// forEach +console.time('forEach'); +numbers.forEach((num) => + console.log(num)); +console.timeEnd('forEach'); diff --git a/25_Day_World_countries_data_visualization_1/25_day_starter/scripts/main.js b/25_Day_World_countries_data_visualization_1/25_day_starter/scripts/main.js index c6045c8..5bc0832 100644 --- a/25_Day_World_countries_data_visualization_1/25_day_starter/scripts/main.js +++ b/25_Day_World_countries_data_visualization_1/25_day_starter/scripts/main.js @@ -1,2 +1,2 @@ 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') \ No newline at end of file