How to Count All the Letters from a String in JavaScript

How to Count All the Letters from a String in JavaScript

Introduction

In this tutorial we will create a How to Count All the Letters from a String in JavaScript. This tutorial purpose is to teach you how to count letters from a string. This will tackle all the important functionality that will count a string. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you want to count a string. I will give my best to provide you the easiest way of creating this program Count Letters from a string. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html forms and the result area. To create this simply copy and write it into your text editor, then save it 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="navabr navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <br />
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">How to Count All the Letters from a String</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-7">
  18.                         <h2>RESULT</h2>
  19.                         <div id="result"></div>
  20.                 </div>
  21.                 <div class="col-md-5">
  22.                         <div class="form-group">
  23.                                 <textarea class="form-control" style="resize:none; height:150px;" id="string"></textarea>
  24.                         </div>
  25.                         <button type="button" class="btn btn-primary">Count</button>
  26.                 </div>
  27.         </div>
  28. <script src="script.js"></script>
  29. </body>
  30. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow to count the letter from the entered string when you clicked the button. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. const submitButton = document.querySelector(".btn-primary");
  2.  
  3.  
  4. submitButton.addEventListener("click", countString);
  5.  
  6.  
  7.  
  8. function countString(){
  9.         const str=document.getElementById("string").value;
  10.         let letters = 0;
  11.         if(str==""){
  12.                 alert("Please enter something first!");
  13.         }else{
  14.                 const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  15.                 let ar = alphabet.split("");
  16.                 for (var i=0; i<str.length;i++) {
  17.                         if (ar.indexOf(str[i]) > -1) {
  18.                                 letters = letters + 1;
  19.                         }
  20.                 }
  21.         }
  22.  
  23.         document.getElementById('result').innerHTML="<center><h4>There are total of <span class='text-primary'>"+letters+"</span> letters in a string.</h4></center>";
  24. }

In the code above we just simply create a method that will the total letters in your entered string called countString(). This function will automatically count the letters from your string and display it in the result section. This code use indexOf() function that will count the letters base on the index position of a string.

Output:

The How to Count All the Letters from a String in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Count All the Letters from a String in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment