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"))
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"))
Share
Improve this question
edited Feb 8, 2018 at 6:47
Pang
10.1k146 gold badges86 silver badges124 bronze badges
asked Feb 8, 2018 at 5:17
ghostface78ghostface78
511 gold badge2 silver badges4 bronze badges
1
- Possible duplicate of Remove ALL white spaces from text – xskxzr Commented Feb 8, 2018 at 6:06
10 Answers
Reset to default 12Simply You can do this,
let str = ' aaa aa ';
str = str.replace(/\s+/g,'');
console.log(str);
Hope this code will help you
var str = " this is a sample "
var res = str.replace(/ /g, "");
console.log(res);
O/P - 'thisisasample'
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.
You can try this:
word = word.replace(/\s+/g, " ").trim();
It will remove the spaces from the front and the back as well.
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"));
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, "");
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"))
Please use the following code to remove space between two words.
word = word.replace(/\s/g, ''));
You can try this.
replace(/\s+/, " ")
Why are they all using the arcane regex syntax when you can simply do:
word = word.replace(' ', '');