/*
Write a function called countChars that accepts two parameters: a string and a character. This function should return a number representing the number of times that the character appears in string. To access the first element of a string, you can use the following syntax:
access the element at index 0
“hello”[0]; // => “h”
“dog”[0]; // => “d”
HINT: You’ll also need to make use of the slice method
*/
It’s a simple question I know but what I do from now was
function countChars(string, character) {
var pattern = new RegExp(character);
return (string.match(pattern).length);
}
countChars(‘doggie’,‘g’);
countChars(‘terrarium’,‘r’);
it yields 1,1 .
can anyone help me how to solve this?