From da62aab4e9b406494a76245e399e04e7a10b0f7f Mon Sep 17 00:00:00 2001 From: Jose Linardo Date: Wed, 29 Jun 2022 18:46:17 -0500 Subject: [PATCH] dia 12 done --- .../11_day_starter/scripts/main.js | 11 +- .../12_day_starter/scripts/main.js | 133 +++++++++++++++++- 2 files changed, 139 insertions(+), 5 deletions(-) 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..a14d640 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,133 @@ + 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'); + 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)); + + +