Problem overview
The method countVowels receives a string s and returns how many vowels it contains. The vowels are a, e, i, o and u, in lower case or upper case. The letter y is not a vowel here. Everything else, whether consonants, spaces, digits or punctuation, is skipped.
In "Hello World" the vowels are e, o and o, so the answer is 3. In "AlgoCademy" they are A, o, a and e, so the answer is 4: the upper-case A counts and the y doesn't. "Why try?" has none, so the answer is 0.
Approach: look at each character and ask a yes or no question
The method has to examine every character once and keep a tally. For each character the question is the same: is it one of the ten vowel characters? A string holding exactly those ten characters, "aeiouAEIOU", works as the list of allowed values. Both cases are written out, so no case conversion is needed.
The question "does this string contain this character?" is what the string method includes answers: "aeiouAEIOU".includes("e") is true, and "aeiouAEIOU".includes("y") is false.
Step by step
- Store the vowels in a
constnamedvowels:"aeiouAEIOU". It never changes, soconstsays so. The letter y is left out on purpose. - Declare
countwithlet, starting at 0. It changes inside the loop. - Loop over the string with
for (let c of s). Afor...ofloop over a string visits each character in order, andcholds the current one as a one-character string. - Test the character. If
vowels.includes(c)istrue, add 1 tocountwith++. Consonants, spaces, digits and punctuation aren't invowels, so they change nothing. - Return
countafter the loop. Returning inside the loop would stop at the first character.
The solution in JavaScript
class Solution {
countVowels(s) {
const vowels = "aeiouAEIOU";
let count = 0;
for (let c of s) {
if (vowels.includes(c)) {
count++;
}
}
return count;
}
}
Details worth knowing:
for...ofagainstfor...in.for (let c of s)gives the characters themselves.for (let i in s)would give the positions as strings, "0", "1", and so on, which are not what the check needs.- There is no separate character type. A character is just a string of length 1, so
ccan be passed straight toincludes. includeslooks for a substring. That is fine here, sincecis a single character. It also means that"aeiouAEIOU".includes("")and"aeiouAEIOU".includes("ae")are bothtrue, so the method must never pass anything longer than one character.- The check is case-sensitive.
"E"is a different character from"e", which is why both cases are invowels. Another option is to lower-case first withs.toLowerCase()and keep only five letters in the string.
Edge cases and common mistakes
- No vowels.
"Why try?"returns 0, and so does"xyz". - Only vowels.
"AEIOU aeiou"returns 10: the space is skipped and each of the ten letters counts. - Forgetting upper case. A vowels string of only
"aeiou"would miss the capital A in"AlgoCademy"and return 3 instead of 4. - Counting
y. Adding it to the vowel list changes the answers for words like"Why try?", which would give 2 instead of 0. - Returning too early. A
returninside the loop ends the method at the first character. - Counting distinct vowels. The task counts every occurrence.
"Hello World"has two o's, and both count.
Testing
s = "Hello World"gives3.s = "AlgoCademy"gives4.s = "Why try?"gives0.s = "xyz"gives0.s = "AEIOU aeiou"gives10.
