Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions projects/m4/006-two-word-random-password/js/index.js
Original file line number Diff line number Diff line change
@@ -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);
24 changes: 24 additions & 0 deletions projects/m4/006-two-word-random-password/js/sample_file.txt
Original file line number Diff line number Diff line change
@@ -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**
24 changes: 24 additions & 0 deletions projects/m4/006-two-word-random-password/sample_file.txt
Original file line number Diff line number Diff line change
@@ -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**