How to calculate string length without spaces in Node.js

1 Answer

0 votes
function lengthWithoutSpaces(str) {
    // Remove all spaces using a regular expression
    const stringWithoutSpaces = str.replace(/\s+/g, '');
 
    return stringWithoutSpaces.length;
}
 
const str = "Node.js is a JavaScript runtime built on Chrome V8";
 
const length = lengthWithoutSpaces(str);
 
console.log(length); 
 
 
  
  
/*
run:
 
42
  
*/

 



answered Jan 11, 2025 by avibootz

Related questions

1 answer 107 views
1 answer 103 views
1 answer 111 views
1 answer 106 views
1 answer 86 views
...