👨‍💻 Daily Code 44 | Count Vowels in 🐍 Python and 🟨 JavaScript

👨‍💻 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()

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);
}

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);
}

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}`);

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>

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