This file contains hidden or 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
Write a function called countVowels that takes a string as input and returns the count of vowels (a, e, i, o, u) in the string. The function should be case-insensitive, meaning it should count both uppercase and lowercase vowels.
- The function countVowels is defined, which takes a single parameter string, representing the input string for which we want to count the vowels.
- Inside the function, a new variable str is declared and assigned the value of the input string converted to lowercase using the toLowerCase() method. This ensures that the function is case-insensitive and will count both uppercase and lowercase vowels.
- Another variable vowelCount is declared and initialized to 0. This variable will be used to keep track of the count of vowels in the input string.
- The function enters a for loop, iterating over each character of the str string. The loop starts from i = 0 and continues until i is less than the length of the string str.
- Inside the loop, there's an if statement that checks if the current character str[i] is equal to any of the vowels 'a', 'e', 'i', 'o', or 'u'. If the condition is true (i.e., if str[i] is a vowel), then the vowelCount variable is incremented by 1.
- After the loop finishes iterating through all characters in the string, the function returns the value of vowelCount, which represents the total count of vowels found in the input string.
Solution #2
- The function countVowels is defined, which takes a single parameter str, representing the input string for which we want to count the vowels.
- Inside the function, the input string str is converted to lowercase using the toLowerCase() method. This ensures that the function is case-insensitive and will count both uppercase and lowercase vowels.
- An array vowels is defined, containing the vowels 'a', 'e', 'i', 'o', and 'u'. This array will be used to check if a character in the string is a vowel.
- Another variable vowelCount is declared and initialized to 0. This variable will be used to keep track of the count of vowels in the input string.
- The function enters a for loop, iterating over each character of the str string. The loop starts from i = 0 and continues until i is less than the length of the string str.
- Inside the loop, there's an if statement that checks if the current character str[i] is included in the vowels array using the includes method. If the condition is true (i.e., if str[i] is a vowel), then the vowelCount variable is incremented by 1.
- After the loop finishes iterating through all characters in the string, the function returns the value of vowelCount, which represents the total count of vowels found in the input string.
## References
Below is a reference to a Stack Overflow discussion on alternative approaches to this problem.
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Added countVowels problem #68
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Added countVowels problem #68
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing