Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Improved capitalization. Improved formatting.
Source Link
Pang
  • 10.2k
  • 146
  • 87
  • 126

Remove Whitespacewhitespace in a string with javascriptJavaScript

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

}

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

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

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")) 
Source Link
ghostface78
  • 51
  • 1
  • 2
  • 4

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