-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3410 from Zerchito/reto/19-zerch
Reto #19 - TypeScript
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Retos/Reto #19 - ANÁLISIS DE TEXTO [Media]/typescript/zerchito.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |