1

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"))
1

10 Answers 10

12

Simply You can do this,

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

Sign up to request clarification or add additional context in comments.

Comments

2

Hope this code will help you

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

O/P - 'thisisasample'

Comments

1

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.

Comments

1

You can try this:

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

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

1 Comment

You don't need to trim() it as your regex will match the front and back whitespaces too.
1

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"));

Comments

0
 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, "");     

Comments

0

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"))

Comments

0

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

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

Comments

0

You can try this.

replace(/\s+/, " ")

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.