Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
45 changes: 45 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,45 @@
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 &&

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you perform this check on line 15/16 so that if the password is incorrect, it generates a valid one?
Even better, use a function to achieve this ;-)

secondWordPassword.length >= 3 &&
secondWordPassword.length <= 7 &&

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion line 17/18.

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');
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can try to solve the problem and do some refactoring during the mentorship ;-)

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**