DEV Community

Cover image for ๐Ÿ‘จโ€๐Ÿ’ป Daily Code 44 | Count Vowels in ๐Ÿ Python and ๐ŸŸจ JavaScript
Gregor Schafroth
Gregor Schafroth

Posted on

1

๐Ÿ‘จโ€๐Ÿ’ป Daily Code 44 | Count Vowels in ๐Ÿ Python and ๐ŸŸจ JavaScript

Time for another simple exercise to be solved both in Python and JavaScript!

Here is what I got from ChatGPT:

Exercise: Count Vowels

Write a program that counts the number of vowels in a given string. For this exercise, consider the vowels to be a, e, i, o, and u (you can choose to include or exclude y). The program should take a string as input and output the count of vowels in the string.

For example:

  • Given the string "hello world", the program should output 3 (since there are three vowels: e, o, o).

This exercise will help you practice string manipulation, looping, and conditionals in both Python and JavaScript.

Here's a breakdown of the task:

  1. Iterate over each character in the string.
  2. Check if the character is a vowel.
  3. Keep a count of the number of vowels.
  4. Output the final count.

Remember, the logic will be similar in both languages, but the syntax will differ. Give it a try, and see how you can implement this in both Python and JavaScript.

My Code

Alright, letโ€™s give this a try and get started with Python!

# Exercise: Count Vowels

def main():
    user_input = input('Input: ')
    print(f'Vowels: {countVowels(user_input)}')

def countVowels(string):
    count = 0
    vowels = "aeiou"
    for letter in string:
        if letter.lower() in vowels:        
            count += 1
    return count

if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode

Alright, here I knew how to make the for loop, just had to ask for help with how to check if the individual letters are vowels. Thought I need some special method or library but turns out I can simply define vowels = "aeiou" and then compare it to that. brilliant!

Now JavaScript, this is going to be more difficult for me.

let user_input = 'Hello World';
for (letter in user_input){
    console.log(letter);
}
Enter fullscreen mode Exit fullscreen mode

I tried to start with this. Already doesnโ€™t work because it just gives me numbers instead of the letters. Turns out it can be fixed like this:

let user_input = 'Hello World';
for (let letter of user_input) {
    console.log(letter);
}
Enter fullscreen mode Exit fullscreen mode

And then again got some help from ChatGPT for the include(). canโ€™t just use in like in Python before. also .lower() in Python is .toLocaleLowerCase() in JavaScript.

const user_input = prompt("Input: ");
console.log(`Input: ${user_input}`);
const vowels = 'aeiou';
let count = 0;
for (let letter of user_input) {
    if (vowels.includes(letter.toLocaleLowerCase())) {
        count++;
    }
}
console.log(`Vowels: ${count}`);
Enter fullscreen mode Exit fullscreen mode

And then finally I asked chatGPT to put the whole thing in the browser because I wanted to see how that would work.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Vowel Counter</title>
</head>

<body>
    <input type="text" id="userInput" placeholder="Type a string">
    <button onclick="countVowels()">Count Vowels</button>
    <p id="result"></p>

    <script>
        function countVowels() {
            const userInput = document.getElementById('userInput').value;
            const vowels = 'aeiou';
            let count = 0;
            for (let letter of userInput) {
                if (vowels.includes(letter.toLowerCase())) {
                    count++;
                }
            }
            document.getElementById('result').textContent = `Vowels: ${count}`;
        }
    </script>
</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Pheww sounds like a simple exercise but was a tough one again for me. Will ask for even simpler exercises the next few days!

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

๐Ÿ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someoneโ€™s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay