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..305c6eee2 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,57 @@ +const { readFile } = require('fs/promises'); + +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); 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