ghostface78
ghostface78

Reputation: 51

Remove whitespace in a string with JavaScript

I'm trying to get this palindrome generator to work, and I cannot figure out how to get js to remove the white space from between the words in multi-word palindromes. race car keeps coming back false. racecar comes back true, so part of the code is working. How do I get JS to ignore the white space between "race" and "car" so that race car comes back as true?

function palindrome(word) {
    var len = word.length;
    word = word.replace(/ +/g, "");


    for (var i = 0; i < Math.floor(len/2); i++ ) {
      if (word[i] !== word[len - 1 - i]) {
      return "FALSE";
       }
     }
     return "TRUE";
}

console.log(palindrome("race car"))

Upvotes: 1

Views: 20856

Answers (10)

Moby Dick
Moby Dick

Reputation: 1

Why are they all using the arcane regex syntax when you can simply do:

word = word.replace(' ', '');

Upvotes: 0

Aydın Gaziulusoy
Aydın Gaziulusoy

Reputation: 11

Hope, this helps:

function palindrome(str) {
  str = str.replace(/\s+/g, ''); // Delete whitespaces.
  return str.split('') // Convert the string into an array.
    .reverse()
    .join('') === str; // Convert the array into the string.
}
console.log(palindrome("race car"));

Upvotes: 1

Raja Mohamed
Raja Mohamed

Reputation: 1026

Hope this code will help you

var str = " this is a sample "
var res = str.replace(/ /g, "");
console.log(res);

O/P - 'thisisasample'

Upvotes: 2

Mayur Phanse
Mayur Phanse

Reputation: 23

You can try this.

replace(/\s+/, " ")

Upvotes: 0

Innocent Criminal
Innocent Criminal

Reputation: 306

You can try this:

word = word.replace(/\s+/g, " ").trim();

It will remove the spaces from the front and the back as well.

Upvotes: 1

Rajesh Kumar Swain
Rajesh Kumar Swain

Reputation: 121

Please use the following code to remove space between two words.

word = word.replace(/\s/g, ''));

Upvotes: 0

ThunderStorm
ThunderStorm

Reputation: 46

You are taking len before removing white spaces. So the last element you were asking returning undefined, that's result in no match and FALSE output.

Try following snippet:

 function palindrome(word) {
          word = word.replace(/ +/g, "");
          var len = word.length;
          for (var i = 0; i < Math.floor(len/2); i++ ) {
          if (word[i] !== word[len - 1 - i]) {
          return "FALSE";
           }
         }
         return "TRUE";
    }
 console.log(palindrome("race car"))

Upvotes: 0

Hussain Ali Akbar
Hussain Ali Akbar

Reputation: 1655

You can do this with a regex before passing it to the palindrome function:

'race car'.replace(/\s+/, "") 

'race car' can also be replaced by any variable containing your string.

Upvotes: 1

vibhor1997a
vibhor1997a

Reputation: 2376

Simply You can do this,

let str = '  aaa  aa  ';
str = str.replace(/\s+/g,'');
console.log(str);

Upvotes: 12

xianshenglu
xianshenglu

Reputation: 5369

 var len = word.length; 

get the len after word change not before;

function palindrome(word) {

  word = word.replace(/ +/g, "");
  var len = word.length;

  for (var i = 0; i < Math.floor(len / 2); i++) {
    if (word[i] !== word[len - 1 - i]) {
      return "FALSE";
    }
  }
  return "TRUE";
}
console.log(palindrome("race car"))

and

  word = word.replace(/ +/g, "");

suggest use regular expression:

  word = word.replace(/\s+/g, "");     

Upvotes: 0

Related Questions