javascript - How to remove spaces and special characters from string? -


i have function returns true if character form of punctuation , i'm trying write function accepts string , removes spaces , punctuation marks while calling first function. got of think. i'm stuck. appreciated.

var ispunct = function(ch) {      if (ch = ch.match(/[,.!?;:'-]/g))         return true     else         return false }  //b  var compress = function(s) {     var result = "";      //loop traverse s     (var = 0; < s.length; i++) {         if (!(ispunct(ch));             //(ispunct(s.charat(i) || s.charat(i) == " "));             //do nothing             else                 result = result + !compress(i)         }          return result     } 

some issues:

  • the inner condition should in fact opposite: want nothing when is punctuation character, i.e. don't want add result. in other case want that.

  • the call !compress(i) wrong: first of function expects string, not index, , returns string, not boolean (so perform ! on it). seems want call function recursively, , although option, also iterating on string. should 1 of two: recursion or iteration.

  • you reference variable ch in compress function have not defined there.

so, if want write compress iteration way, change code follows:

var compress = function(s) {     var result = "", ch; // define ch.      //loop traverse s     (var = 0; < s.length; i++) {         ch = s[i]; // initialise ch.         if (!ispunct(ch)) result = result + ch; // add when not punctuation     }     return result; } 

if on other hand want keep recursive call compress, should away for loop:

var compress = function(s) {     var result = "", ch, rest;      if (s.length == 0) return '';     result = compress(s.substr(1)); // recursive call     ch = s[0];     if (!ispunct(ch)) result = ch + result;     return result; } 

the function ispunct has strange thing happening: assigns boolean value ch in if expression. not make function malfunction, assignment serves no purpose: match method returns boolean need if condition.

it not nice-looking first evaluate boolean expression in if return same value in form of false , true. can returning evaluated expression itself:

var ispunct = function(ch) {     return ch.match(/[,.!?;:'-]/g); } 

on final note, don't need ispunct function if use in compress. whole logic can performed in 1 function only, this:

let compress = s => s.replace(/[,.!?;:'-]/g,'');  // demo:    console.log(compress('a,b,c')); // abc

if prefer keep ispunct , don't want repeat regular expression elsewhere, can replace this:

let ispunct = ch => ch.match(/[,.!?;:'-]/g);    let compress = s => array.from(s).filter(ch => !ispunct(ch)).join('');  // demo:    console.log(compress('a,b,c')); // abc

note how use of es6 arrow functions , es5 array methods makes code quite lean.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -