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
24 changes: 24 additions & 0 deletions projects/m4/008-a-book-with-no-e/js/example1.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**
52 changes: 52 additions & 0 deletions projects/m4/008-a-book-with-no-e/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { readFile } = require('fs/promises');
const filePath = './example1.txt';

const getLettersWords = (data) => {
const arr = data.toLowerCase().split(/\s+/);
const deleteDuplicates = [...arr].map((item) =>
[...new Set(item)].join('')
);

console.log(deleteDuplicates);
const alphabetLetters = [...'abcdefghijklmnopqrstuvwxyz']; // Lettere dell'alfabeto
let count = {}; // Oggetto per memorizzare il conteggio delle lettere

alphabetLetters.forEach((letter) => {
count[letter] = 0; // Inizializza il contatore a 0 per ogni lettera
});

// Conta le occorrenze di ciascuna lettera
deleteDuplicates.forEach((word) => {
word.split('').forEach((char) => {
if (alphabetLetters.includes(char)) {
count[char] = (count[char] || 0) + 1; // Incrementa il conteggio della lettera
}
});
});

// Calcola la somma totale delle lettere contate
const totalSumLetters = Object.values(count).reduce(
(sum, num) => sum + num,
0
);

// Calcola la percentuale di ciascuna lettera
let proportionEachLetter = {};
for (let [letter, num] of Object.entries(count)) {
proportionEachLetter[letter] =
((num / totalSumLetters) * 100).toFixed(2) + '%';
}

// Restituisce sia il conteggio che le percentuali
return { count, proportionEachLetter };
};

readFile(filePath, 'utf8')
.then((data) => {
const { count, proportionEachLetter } = getLettersWords(data);
console.log(count);
console.log(proportionEachLetter);
})
.catch((err) => {
console.error('Errore nella lettura del file:', err); // Gestisce eventuali errori
});