
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Total Punctuations in a String in JavaScript
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
Advertisements