From c8838970363cc4a5ad8cdda191023cac96e1a851 Mon Sep 17 00:00:00 2001 From: "Sergio Flores Ledezma (zerch)" Date: Thu, 11 May 2023 23:02:26 -0400 Subject: [PATCH] Reto #19 - TypeScript --- .../typescript/zerchito.ts" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 "Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/typescript/zerchito.ts" diff --git "a/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/typescript/zerchito.ts" "b/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/typescript/zerchito.ts" new file mode 100644 index 0000000000..4dd44fa5b8 --- /dev/null +++ "b/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/typescript/zerchito.ts" @@ -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') +} \ No newline at end of file