FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Added countVowels problem by henryarpon · Pull Request #68 · devvsakib/javascript-problem-solving · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (2) .md  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
94 changes: 94 additions & 0 deletions L-B/0027 countVowels ( L-B )/README.md
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# 0027 countVowels ( L-B )


## Problem
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.

## Solution

```
//There are two ways to solve this problem

//Solution #1
function countVowels(string) {

const str = string.toLowerCase();

let vowelCount = 0;

for (let i = 0; i < str.length; i++) {

if (str[i] === 'a' || str[i] === 'e' || str[i] === 'i' || str[i] === 'o' || str[i] === 'u') {
vowelCount++;
}
}

return vowelCount;
}

//Solution #2
function countVowels(str) {

str = str.toLowerCase();

const vowels = ['a', 'e', 'i', 'o', 'u'];

let vowelCount = 0;

for (let i = 0; i < str.length; i++) {

if (vowels.includes(str[i])) {
vowelCount++;
}
}

return vowelCount;
}
```

## How it works

Solution #1
- 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.
- [StackOverflow](https://stackoverflow.com/questions/29450399/counting-number-of-vowels-in-a-string-with-javascript)


## Problem Added By
Same as reference, use your any social links
- [GitHub](https://github.com/henryarpon)
- [LinkedIn](https://www.linkedin.com/in/henryarpon/)

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.
15 changes: 15 additions & 0 deletions L-B/0027 countVowels ( L-B )/countVowels_1.js
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function countVowels(string) {

const str = string.toLowerCase();

let vowelCount = 0;

for (let i = 0; i < str.length; i++) {

if (str[i] === 'a' || str[i] === 'e' || str[i] === 'i' || str[i] === 'o' || str[i] === 'u') {
vowelCount++;
}
}

return vowelCount;
}
17 changes: 17 additions & 0 deletions L-B/0027 countVowels ( L-B )/countVowels_2.js
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function countVowels(str) {

str = str.toLowerCase();

const vowels = ['a', 'e', 'i', 'o', 'u'];

let vowelCount = 0;

for (let i = 0; i < str.length; i++) {

if (vowels.includes(str[i])) {
vowelCount++;
}
}

return vowelCount;
}

Back | FazBrowse Home | New Git URL