Science Stream Practicals

Std 12th IT Subject - Skill Oriented Practical (Science Stream)

SOP 3: Create javascript program to count no of vowels in a string

Create event driven JavaScript program for the following. Make use of appropriate variables, JavaScript inbuilt string functions and control structures:

Source Code: index.html

<!DOCTYPE html>
<html>
<head>
    <title>SOP 3 - Count Vowels</title>
</head>
<body>
    <h2>Count Vowels in a String</h2>
    <form name="f1">
        <input type="text" name="String">
        <input type="button" value="Check No of vowels" onclick="checkvowels()">
    </form>
    <script>
        function checkvowels() {
            var str = f1.String.value, count = 0;
            str = str.toLowerCase();
            for (var i = 0; i < str.length; i++) {
                ch=str[i];
                if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                    count++;
                }
            }
            alert("Number of vowels: " + count);
        }
    </script>
</body>
</html>

Live Preview