JavaScript - Count Vowels and Consonant in a Sentence

In this tutorial we will create a Count Vowels and Consonant in a Sentence using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding..

Getting started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Count Vowels and Consonant in a Sentence</h3>
  15. <hr style="border-top:1px dotted;"/>
  16. <div class="col-md-6">
  17. <div class="form-group">
  18. <textarea id="words" class="form-control" style="height:150px; resize:none;"></textarea>
  19. </div>
  20. <button onclick="countCheck();" class="btn btn-primary"><span class="glyphicon glyphicon-eye-open"></span> Check</button>
  21. <button onclick="reset();" class="btn btn-success"><span class="glyphicon glyphicon-refresh"></span> Reset</button>
  22. </div>
  23. <div class="col-md-6">
  24. <div id="result">
  25. </div>
  26. </dvi>
  27. </div>
  28. <script src="js/script.js"></script>
  29. </body>
  30. </html>

Creating the Script

This code contains the script of the application.This code will count all the vowels and consonant in a word. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. function reset(){
  2. document.getElementById('words').value = "";
  3. document.getElementById('result').innerHTML = "";
  4. }
  5.  
  6. function countCheck(){
  7. var words = document.getElementById('words').value;
  8.  
  9. if(words == ""){
  10. document.getElementById('result').innerHTML = "<center><label class='text-danger'>Please enter some word</label></center>";
  11. }else{
  12. document.getElementById('result').innerHTML = "<label style='font-size:24px;'>Vowels:</label> <label style='font-size:20px;' class='text-success'>"+vowels(words)+"</label><br /><label style='font-size:24px;'>Consonant:</label> <label style='font-size:20px;' class='text-success'>"+consonant(words)+"</label>";
  13. }
  14. }
  15.  
  16. function vowels(str){
  17. var vowels = "AEIOUaeiou";
  18. var count = 0;
  19.  
  20. for(var i=0; i < str.length; i++){
  21. if(vowels.indexOf(str[i]) !== -1){
  22. count++;
  23. }
  24. }
  25.  
  26. return count;
  27. }
  28.  
  29. function consonant(str){
  30. var consonant = "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz";
  31. var count = 0;
  32.  
  33. for(var i=0; i < str.length; i++){
  34. if(consonant.indexOf(str[i]) !== -1){
  35. count++;
  36. }
  37. }
  38.  
  39. return count;
  40. }
There you have it we successfully created a Count Vowels and Consonant in a Sentence using Javascript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment