English

Write event driven JavaScript program for the following. Accept any string from user and count and display number of vowels occurs in it. - Information Technology

Advertisements
Advertisements

Question

Write event driven JavaScript program for the following.

Accept any string from user and count and display number of vowels occurs in it.

Answer in Brief

Solution

Coding:

<html>
<script type="text/javascript">
    var n, i, ch, cnt = 0;
    n = prompt("Enter a String");
    for (i = 0; i < n.length; i++) {
      ch = n.charAt(i);
      if (ch == 'a' || ch == ' A' || ch == 'e' || ch == 'E' || ch == 'i' ||
      ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') {
        cnt = cnt + 1;
      }
    }
    document.write("Number of vowels in string are " + cnt);
</script>
</html>
shaalaa.com
JavaScript Built-in Objects - String
  Is there an error in this question or solution?
Chapter 3: Advanced Javascript - Exercises [Page 51]

APPEARS IN

Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×