Skip to content

Commit

Permalink
Merge pull request #3410 from Zerchito/reto/19-zerch
Browse files Browse the repository at this point in the history
Reto #19 - TypeScript
  • Loading branch information
Roswell468 authored May 13, 2023
2 parents 9d1c51f + c883897 commit 01824d0
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Retos/Reto #19 - ANÁLISIS DE TEXTO [Media]/typescript/zerchito.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Crea un programa que analice texto y obtenga:
* - Número total de palabras.
* - Longitud media de las palabras.
* - Número de oraciones del texto (cada vez que aparecen un punto).
* - Encuentre la palabra más larga.
*
* Todo esto utilizando un único bucle.
*/

{
function analyzeText(text: string): void {
text = text.trim();
let wordsCount = 0;
let wordsLenght= 0;
let numberOfSentences = 0;
let longestWord = '';
let currentWord = '';
for(let index = 0; index < text.length; index++) {
const char = text.charAt(index);
if(char === ' '){
if(currentWord.length > 0) {
wordsCount++;
console.log(currentWord.length)
wordsLenght += currentWord.length;
longestWord = currentWord.length > longestWord.length ? currentWord : longestWord;
}
currentWord = '';
} else if (char === '.') {
if(currentWord.length > 0) {
wordsCount++;
console.log(currentWord.length)
wordsLenght += currentWord.length;
longestWord = currentWord.length > longestWord.length ? currentWord : longestWord;
numberOfSentences++;
}
currentWord = '';
} else {
currentWord += char;
}
}
if(currentWord.length > 0) {
wordsCount++;
console.log(currentWord.length)
wordsLenght += currentWord.length;
longestWord = currentWord.length > longestWord.length ? currentWord : longestWord;
numberOfSentences++;
}
console.log(`Number of words: ${wordsCount}`);
console.log(`Words lenght media: ${wordsLenght / wordsCount}`);
console.log(`Number of sentences: ${numberOfSentences}`);
console.log(`Longest word: ${longestWord}`);
}

analyzeText('Esta es una prueba. para ver si el analizador funciona. hehe. Hola ten un lindo dia')
}

0 comments on commit 01824d0

Please sign in to comment.