In the English language, all these characters are considered as punctuations −
'!', "," ,"\'" ,";" ,"\"", ".", "-" ,"?"
We are required to write a JavaScript function that takes in a string and count the number of appearances of these punctuations in the string and return that count.
Example
Let’s write the code for this function −
const str = "This, is a-sentence;.Is this a sentence?"; const countPunctuation = str => { const punct = "!,\;\.-?"; let count = 0; for(let i = 0; i < str.length; i++){ if(!punct.includes(str[i])){ continue; }; count++; }; return count; }; console.log(countPunctuation(str));
Output
The output in the console: −
5