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/5] 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/5] 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/5] 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 59d3fc5aaea760dd8a670b15504c4883256deda9 Mon Sep 17 00:00:00 2001 From: AlessandroVerzella Date: Wed, 30 Oct 2024 18:13:37 +0100 Subject: [PATCH 4/5] Added example01.txt --- .../m4/008-a-book-with-no-e/js/exmple1.txt | 0 projects/m4/008-a-book-with-no-e/js/index.js | 21 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 projects/m4/008-a-book-with-no-e/js/exmple1.txt diff --git a/projects/m4/008-a-book-with-no-e/js/exmple1.txt b/projects/m4/008-a-book-with-no-e/js/exmple1.txt new file mode 100644 index 000000000..e69de29bb diff --git a/projects/m4/008-a-book-with-no-e/js/index.js b/projects/m4/008-a-book-with-no-e/js/index.js index e69de29bb..39399f9d8 100644 --- a/projects/m4/008-a-book-with-no-e/js/index.js +++ b/projects/m4/008-a-book-with-no-e/js/index.js @@ -0,0 +1,21 @@ +const prompt = require('prompt-sync')(); + +const arr = ['alessandro', 'ciao', 'come', 'stai', 'stai']; +const set = [...arr].map((item) => [...new Set(item)].join('')); // Rimuove duplicati senza spazi + +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 +}); + +set.forEach((word) => { + word.split('').forEach((char) => { + if (alphabetLetters.includes(char)) { + count[char] = (count[char] || 0) + 1; // Incrementa il conteggio della lettera + } + }); +}); + +console.log(count); From a9ae1a8210e6e1bbd92706fb62409d03a86db53f Mon Sep 17 00:00:00 2001 From: AlessandroVerzella Date: Sat, 2 Nov 2024 12:52:06 +0100 Subject: [PATCH 5/5] Implemented solution for getLettersWords function and added comments for code clarity --- .../m4/008-a-book-with-no-e/js/example1.txt | 24 ++++++++ .../m4/008-a-book-with-no-e/js/exmple1.txt | 0 projects/m4/008-a-book-with-no-e/js/index.js | 61 ++++++++++++++----- 3 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 projects/m4/008-a-book-with-no-e/js/example1.txt delete mode 100644 projects/m4/008-a-book-with-no-e/js/exmple1.txt diff --git a/projects/m4/008-a-book-with-no-e/js/example1.txt b/projects/m4/008-a-book-with-no-e/js/example1.txt new file mode 100644 index 000000000..364e35b8e --- /dev/null +++ b/projects/m4/008-a-book-with-no-e/js/example1.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/008-a-book-with-no-e/js/exmple1.txt b/projects/m4/008-a-book-with-no-e/js/exmple1.txt deleted file mode 100644 index e69de29bb..000000000 diff --git a/projects/m4/008-a-book-with-no-e/js/index.js b/projects/m4/008-a-book-with-no-e/js/index.js index 39399f9d8..20b0f0f7c 100644 --- a/projects/m4/008-a-book-with-no-e/js/index.js +++ b/projects/m4/008-a-book-with-no-e/js/index.js @@ -1,21 +1,52 @@ -const prompt = require('prompt-sync')(); +const { readFile } = require('fs/promises'); +const filePath = './example1.txt'; -const arr = ['alessandro', 'ciao', 'come', 'stai', 'stai']; -const set = [...arr].map((item) => [...new Set(item)].join('')); // Rimuove duplicati senza spazi +const getLettersWords = (data) => { + const arr = data.toLowerCase().split(/\s+/); + const deleteDuplicates = [...arr].map((item) => + [...new Set(item)].join('') + ); -const alphabetLetters = [...'abcdefghijklmnopqrstuvwxyz']; // Lettere dell'alfabeto -let count = {}; // Oggetto per memorizzare il conteggio delle lettere + 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 -}); + alphabetLetters.forEach((letter) => { + count[letter] = 0; // Inizializza il contatore a 0 per ogni lettera + }); -set.forEach((word) => { - word.split('').forEach((char) => { - if (alphabetLetters.includes(char)) { - count[char] = (count[char] || 0) + 1; // Incrementa il conteggio della 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 + } + }); }); -}); -console.log(count); + // 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 + });