From c1bf6ab53f0065af09ee7cd090779ba5c9ad0828 Mon Sep 17 00:00:00 2001 From: AlessandroVerzella <116301354+AlessandroVerzella@users.noreply.github.com> Date: Sat, 15 Jul 2023 12:10:56 +0200 Subject: [PATCH 1/7] added a console.log instruction --- projects/m1/001-area-of-a-room/js/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/projects/m1/001-area-of-a-room/js/index.js b/projects/m1/001-area-of-a-room/js/index.js index e69de29bb..66c7ece20 100644 --- a/projects/m1/001-area-of-a-room/js/index.js +++ b/projects/m1/001-area-of-a-room/js/index.js @@ -0,0 +1 @@ +console.log('aaa'); From 66f5b5d19e2ea09f38877109b2d91f2719ce10de Mon Sep 17 00:00:00 2001 From: AlessandroVerzella <116301354+AlessandroVerzella@users.noreply.github.com> Date: Sat, 15 Jul 2023 12:13:16 +0200 Subject: [PATCH 2/7] add a second console log --- projects/m1/001-area-of-a-room/js/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/projects/m1/001-area-of-a-room/js/index.js b/projects/m1/001-area-of-a-room/js/index.js index 66c7ece20..a1be9412c 100644 --- a/projects/m1/001-area-of-a-room/js/index.js +++ b/projects/m1/001-area-of-a-room/js/index.js @@ -1 +1,3 @@ console.log('aaa'); + +console.log('bbb'); From fb2e8d5b625f99798a1c151b734e3fd13e101d59 Mon Sep 17 00:00:00 2001 From: AlessandroVerzella <116301354+AlessandroVerzella@users.noreply.github.com> Date: Sat, 15 Jul 2023 12:15:02 +0200 Subject: [PATCH 3/7] removed console.logs --- projects/m1/001-area-of-a-room/js/index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/projects/m1/001-area-of-a-room/js/index.js b/projects/m1/001-area-of-a-room/js/index.js index a1be9412c..e69de29bb 100644 --- a/projects/m1/001-area-of-a-room/js/index.js +++ b/projects/m1/001-area-of-a-room/js/index.js @@ -1,3 +0,0 @@ -console.log('aaa'); - -console.log('bbb'); From c08b27cbf3bb2b32c088ddfa9b520adf0dd45354 Mon Sep 17 00:00:00 2001 From: AlessandroVerzella Date: Mon, 14 Oct 2024 19:52:20 +0200 Subject: [PATCH 4/7] Aggiunto sample_file.txt --- .../js/sample_file.txt | 24 +++++++++++++++++++ .../sample_file.txt | 24 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 projects/m4/006-two-word-random-password/js/sample_file.txt create mode 100644 projects/m4/006-two-word-random-password/sample_file.txt diff --git a/projects/m4/006-two-word-random-password/js/sample_file.txt b/projects/m4/006-two-word-random-password/js/sample_file.txt new file mode 100644 index 000000000..364e35b8e --- /dev/null +++ b/projects/m4/006-two-word-random-password/js/sample_file.txt @@ -0,0 +1,24 @@ +# Two Word Random Password + +While generating a password by selecting random characters usually creates one that is relatively secure, it also generally gives a password that is difficult to memorize. + +As an alternative, some systems construct a password by taking two English words and concatenating them. While this password may not be as secure, it is normally much easier to memorize. + +Write a program that reads a file containing a list of words, randomly selects two +of them, and concatenates them to produce a new password. + +When producing the password ensure that the total length is between 8 and 10 characters, and that each word used is at least three letters long. + +Capitalize each word in the password so that the user can easily see where one word ends and the next one begins. + +Finally, your program should display the password for the user. + +# Documentation + +For this project solution you may use: + +- Files and Exceptions + +# Deadline + +This project requires to be completed in a maximum of **2 hours** \ No newline at end of file diff --git a/projects/m4/006-two-word-random-password/sample_file.txt b/projects/m4/006-two-word-random-password/sample_file.txt new file mode 100644 index 000000000..364e35b8e --- /dev/null +++ b/projects/m4/006-two-word-random-password/sample_file.txt @@ -0,0 +1,24 @@ +# Two Word Random Password + +While generating a password by selecting random characters usually creates one that is relatively secure, it also generally gives a password that is difficult to memorize. + +As an alternative, some systems construct a password by taking two English words and concatenating them. While this password may not be as secure, it is normally much easier to memorize. + +Write a program that reads a file containing a list of words, randomly selects two +of them, and concatenates them to produce a new password. + +When producing the password ensure that the total length is between 8 and 10 characters, and that each word used is at least three letters long. + +Capitalize each word in the password so that the user can easily see where one word ends and the next one begins. + +Finally, your program should display the password for the user. + +# Documentation + +For this project solution you may use: + +- Files and Exceptions + +# Deadline + +This project requires to be completed in a maximum of **2 hours** \ No newline at end of file From 784da327aeb31c5db0afd6f7cff44409b00ad35a Mon Sep 17 00:00:00 2001 From: AlessandroVerzella Date: Wed, 16 Oct 2024 16:51:36 +0200 Subject: [PATCH 5/7] Added solution of the exercise --- .../006-two-word-random-password/js/index.js | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/projects/m4/006-two-word-random-password/js/index.js b/projects/m4/006-two-word-random-password/js/index.js index e69de29bb..feaed5272 100644 --- a/projects/m4/006-two-word-random-password/js/index.js +++ b/projects/m4/006-two-word-random-password/js/index.js @@ -0,0 +1,89 @@ +const { readFile } = require('fs/promises'); + +let filePath = './sample_file.txt'; + +readFile(filePath) + .then((data) => { + const allTheWordsFile = data + .toString() + .replace(/[0-9.,\/#!$%\^&\*;:{}=\-_`~(),' ', \r\n]/g, ' ') // Remove all characters that are not letters + .split(' ') + .filter((element) => element !== ''); // Don't consider the empty space + + // Pick two random words from the file + const firstWord = + allTheWordsFile[Math.floor(Math.random() * allTheWordsFile.length)]; + const secondWord = + allTheWordsFile[Math.floor(Math.random() * allTheWordsFile.length)]; + + // Capitalise the first letter + const firstWordPassword = + firstWord.charAt(0).toUpperCase() + firstWord.slice(1); + const secondWordPassword = + secondWord.charAt(0).toUpperCase() + secondWord.slice(1); + + // Password with the two random words + const twoWordRandomPassword = + firstWordPassword.concat(secondWordPassword); + + // Check the conditions + if ( + firstWordPassword.length >= 3 && + firstWordPassword.length <= 7 && + secondWordPassword.length >= 3 && + secondWordPassword.length <= 7 && + twoWordRandomPassword.length >= 8 && + twoWordRandomPassword.length <= 10 + ) { + console.log(`The password is ${twoWordRandomPassword}`); + } else { + console.log('Password non valida'); + } + }) + .catch((error) => { + console.log('The program is unable to open the file you indicated'); + }); + +// const prompt = require('prompt-sync'); + +// let arr = ['alessandro', 'ciao', 'come', 'ancora']; +// let num = arr[Math.floor(Math.random() * arr.length)]; +// let num2 = arr[Math.floor(Math.random() * arr.length)]; +// // let firstWord = num; +// // let secondWord = arr[Math.floor(Math.random() * arr.length)]; +// const arr2 = num.charAt(0).toUpperCase() + num.slice(1); +// const arr3 = num2.charAt(0).toUpperCase() + num2.slice(1); +// // console.log(arr2.concat(arr3)); + +// let arr4 = arr2.concat(arr3); +// console.log(arr4); +// console.log(arr2.length); +// console.log(arr3.length); +// console.log(arr4.length); +// // console.log(firstWord, secondWord); + +// // for (let i = 1; i <= 1; i++) { +// // console.log(firstWord, secondWord); +// // } + +// if ( +// arr2.length >= 3 && +// arr2.length <= 7 && +// arr2.length >= 3 && +// arr2.length <= 7 && +// arr4.length >= 8 && +// arr4.length <= 10 +// ) { +// console.log(`The password is ${arr4}`); +// } else { +// console.log('Password non valida'); +// } + +// console.log(arr.length); +// console.log(arr[0]); +// const arr2 = arr[Math.floor(Math.random() * arr.length)]; +// // .charAt(0) +// // .toUpperCase() +// const arr3 = arr[Math.floor(Math.random() * arr.length)]; + +// console.log(arr2.concat(arr3)); From f66a37df05eefb95e706c3e7d4e884ebab62221d Mon Sep 17 00:00:00 2001 From: AlessandroVerzella Date: Wed, 16 Oct 2024 16:52:36 +0200 Subject: [PATCH 6/7] Remove some comments --- .../006-two-word-random-password/js/index.js | 44 ------------------- 1 file changed, 44 deletions(-) diff --git a/projects/m4/006-two-word-random-password/js/index.js b/projects/m4/006-two-word-random-password/js/index.js index feaed5272..ff2845fa8 100644 --- a/projects/m4/006-two-word-random-password/js/index.js +++ b/projects/m4/006-two-word-random-password/js/index.js @@ -43,47 +43,3 @@ readFile(filePath) .catch((error) => { console.log('The program is unable to open the file you indicated'); }); - -// const prompt = require('prompt-sync'); - -// let arr = ['alessandro', 'ciao', 'come', 'ancora']; -// let num = arr[Math.floor(Math.random() * arr.length)]; -// let num2 = arr[Math.floor(Math.random() * arr.length)]; -// // let firstWord = num; -// // let secondWord = arr[Math.floor(Math.random() * arr.length)]; -// const arr2 = num.charAt(0).toUpperCase() + num.slice(1); -// const arr3 = num2.charAt(0).toUpperCase() + num2.slice(1); -// // console.log(arr2.concat(arr3)); - -// let arr4 = arr2.concat(arr3); -// console.log(arr4); -// console.log(arr2.length); -// console.log(arr3.length); -// console.log(arr4.length); -// // console.log(firstWord, secondWord); - -// // for (let i = 1; i <= 1; i++) { -// // console.log(firstWord, secondWord); -// // } - -// if ( -// arr2.length >= 3 && -// arr2.length <= 7 && -// arr2.length >= 3 && -// arr2.length <= 7 && -// arr4.length >= 8 && -// arr4.length <= 10 -// ) { -// console.log(`The password is ${arr4}`); -// } else { -// console.log('Password non valida'); -// } - -// console.log(arr.length); -// console.log(arr[0]); -// const arr2 = arr[Math.floor(Math.random() * arr.length)]; -// // .charAt(0) -// // .toUpperCase() -// const arr3 = arr[Math.floor(Math.random() * arr.length)]; - -// console.log(arr2.concat(arr3)); From c2dc899a281ac431837f8fda385328071f636b9a Mon Sep 17 00:00:00 2001 From: AlessandroVerzella Date: Sat, 19 Oct 2024 16:01:36 +0200 Subject: [PATCH 7/7] Added new solution --- .../006-two-word-random-password/js/index.js | 98 +++++++++++-------- 1 file changed, 55 insertions(+), 43 deletions(-) diff --git a/projects/m4/006-two-word-random-password/js/index.js b/projects/m4/006-two-word-random-password/js/index.js index ff2845fa8..305c6eee2 100644 --- a/projects/m4/006-two-word-random-password/js/index.js +++ b/projects/m4/006-two-word-random-password/js/index.js @@ -1,45 +1,57 @@ const { readFile } = require('fs/promises'); -let filePath = './sample_file.txt'; - -readFile(filePath) - .then((data) => { - const allTheWordsFile = data - .toString() - .replace(/[0-9.,\/#!$%\^&\*;:{}=\-_`~(),' ', \r\n]/g, ' ') // Remove all characters that are not letters - .split(' ') - .filter((element) => element !== ''); // Don't consider the empty space - - // Pick two random words from the file - const firstWord = - allTheWordsFile[Math.floor(Math.random() * allTheWordsFile.length)]; - const secondWord = - allTheWordsFile[Math.floor(Math.random() * allTheWordsFile.length)]; - - // Capitalise the first letter - const firstWordPassword = - firstWord.charAt(0).toUpperCase() + firstWord.slice(1); - const secondWordPassword = - secondWord.charAt(0).toUpperCase() + secondWord.slice(1); - - // Password with the two random words - const twoWordRandomPassword = - firstWordPassword.concat(secondWordPassword); - - // Check the conditions - if ( - firstWordPassword.length >= 3 && - firstWordPassword.length <= 7 && - secondWordPassword.length >= 3 && - secondWordPassword.length <= 7 && - twoWordRandomPassword.length >= 8 && - twoWordRandomPassword.length <= 10 - ) { - console.log(`The password is ${twoWordRandomPassword}`); - } else { - console.log('Password non valida'); - } - }) - .catch((error) => { - console.log('The program is unable to open the file you indicated'); - }); +const filePath = './sample_file.txt'; + +const getWord = (allTheWordsFile) => { + let word = ''; + while (word.length < 3 || word.length > 7) { + const randomIndex = Math.floor(Math.random() * allTheWordsFile.length); + word = allTheWordsFile[randomIndex]; + // console.log(word); + } + // console.log(word.length); + // return word; + return word.charAt(0).toUpperCase() + word.slice(1); +}; + +const getPassword = (allTheWordsFile, firstWord) => { + // Io ottengo una password, che ha dei caratteri da 3 a 7. Firstword.length. + const firstWordLength = firstWord.length; + // So che la lunghezza della password generata deve essere compresa tra 8 e 10; + const minLength = 8; + const maxLength = 10; + // Genero reiterando la seconda password e vedere se, la somma con firstWordLength รจ comprezza tra le due lunghezze + let sumPassword = 0; + let secondWord = 0; + while (sumPassword < minLength || sumPassword > maxLength) { + secondWord = getWord(allTheWordsFile); + const secondWordLength = secondWord.length; + sumPassword = firstWordLength + secondWord.length; + } + + return firstWord + secondWord; +}; + +const generatePasswordFromFile = async (filePath) => { + const data = await readFile(filePath); + const allTheWordsFile = data + .toString() + .replace(/[0-9.,\/#!$%\^&\*;:{}=\-_`~(),' ', \r\n]/g, ' ') // Remove all characters that are not letters + .split(' ') + .filter((element) => element !== ''); + // readFile(filePath) + // .then((data) => { + // Don't consider the empty space + + // Pick two random words from the file + + const firstWord = getWord(allTheWordsFile); + // console.log(firstWord); + const twoWordRandomPassword = getPassword(allTheWordsFile, firstWord); + return twoWordRandomPassword; +}; + +generatePasswordFromFile(filePath) + .then((password) => console.log(password)) + .catch((error) => console.log('Errore')); +// const password = await generatePasswordFromFile(filePath);