11 Sept 2022

Useful Javascript Methods | Javascript method to count the specific char in string

Program:

//method definition

function countString(str, chr){

     var count = 0;

     for(var k=0; k < str.length; k++){

         if(str.charAt(k) === chr){

             count++;

         }

    }

    return count;

}

//method call

countString("Hello World", "l");

Output:

3


No comments:

Post a Comment