Skip to content

Commit 3108f71

Browse files
committed
feat(js): add solution and documentation for easy challenge 8 - Vowel counter
1 parent d9fbf51 commit 3108f71

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Challenge: Create a function that counts the number of vowels in a given string. Consider both uppercase and lowercase letters.
2+
3+
function countVowels(s) {
4+
const vowels = "aeiou";
5+
s = s.toLowerCase();
6+
let count = 0;
7+
for (const char of s) {
8+
if (vowels.includes(char)) {
9+
count++;
10+
}
11+
}
12+
return count;
13+
}
14+
15+
// Example usage
16+
const testStrings = ["Hello", "World", "Python", "OpenAI"];
17+
testStrings.forEach(str => {
18+
console.log(`Number of vowels in '${str}': ${countVowels(str)}`);
19+
});

0 commit comments

Comments
 (0)