How to count the total number of punctuation characters in a string with Node.js

1 Answer

0 votes
const str= "Node.js, is a - programming language? base on ECMAScript specification.";    
 
let count = 0;  
  
for (let i = 0; i < str.length; i++) {  
    if (str[i] == '-' || str[i] == ',' || str[i] == ';' || str[i] == '.' || str[i] == ':' ||   
        str[i] == '!' || str[i] == '\'' || str[i] == '\"' || str[i] == '?') {  
        count++;  
    }  
}  
 
console.log(count);
  
  
  
  
  
/*
run:
  
5
  
*/

 



answered Nov 20, 2022 by avibootz
...