|
| 1 | +# Challenge Description and Solution |
| 2 | + |
| 3 | +## English Version |
| 4 | + |
| 5 | +### Challenge Description |
| 6 | +Create a function that counts the number of vowels in a given string. Consider both uppercase and lowercase letters. |
| 7 | + |
| 8 | +### Code Explanation |
| 9 | +The `countVowels` function converts the string to lowercase and then counts how many characters are vowels using a loop and checking membership in the vowels string. |
| 10 | + |
| 11 | +### Relevant Code Snippet |
| 12 | + |
| 13 | +```javascript |
| 14 | +function countVowels(s) { |
| 15 | + const vowels = "aeiou"; |
| 16 | + s = s.toLowerCase(); |
| 17 | + let count = 0; |
| 18 | + for (const char of s) { |
| 19 | + if (vowels.includes(char)) { |
| 20 | + count++; |
| 21 | + } |
| 22 | + } |
| 23 | + return count; |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +### Example Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +const testStrings = ["Hello", "World", "Python", "OpenAI"]; |
| 31 | +testStrings.forEach(str => { |
| 32 | + console.log(`Number of vowels in '${str}': ${countVowels(str)}`); |
| 33 | +}); |
| 34 | +``` |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## Versión en Español |
| 39 | + |
| 40 | +### Descripción del Reto |
| 41 | +Crea una función que cuente el número de vocales en una cadena dada. Considera tanto mayúsculas como minúsculas. |
| 42 | + |
| 43 | +### Explicación del Código |
| 44 | +La función `countVowels` convierte la cadena a minúsculas y luego cuenta cuántos caracteres son vocales utilizando un bucle y comprobando la pertenencia en la cadena de vocales. |
| 45 | + |
| 46 | +### Fragmento de Código Relevante |
| 47 | + |
| 48 | +```javascript |
| 49 | +function countVowels(s) { |
| 50 | + const vowels = "aeiou"; |
| 51 | + s = s.toLowerCase(); |
| 52 | + let count = 0; |
| 53 | + for (const char of s) { |
| 54 | + if (vowels.includes(char)) { |
| 55 | + count++; |
| 56 | + } |
| 57 | + } |
| 58 | + return count; |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### Ejemplo de Uso |
| 63 | + |
| 64 | +```javascript |
| 65 | +const testStrings = ["Hello", "World", "Python", "OpenAI"]; |
| 66 | +testStrings.forEach(str => { |
| 67 | + console.log(`Número de vocales en '${str}': ${countVowels(str)}`); |
| 68 | +}); |
0 commit comments