85574

Palindrome Program avoiding spaces, punctuation in Javascript

Question:

I'm trying to make a palindrome program using javascript that will show if a string is palindrome or not by <strong>TRUE</strong> or <strong>FALSE</strong> even if the string has punctuation and spaces(ex- madam, i'm adam). But everytime I put a string I only get <strong>TRUE</strong> no matter what the string is. Here is my code,

function isPalindrome (str) { var nopunctuation = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,""); var nospaces = nopunctuation.replace(/\s/g,""); var finalstring = nospaces; var len = finalstring.length; for ( var i = 0; i < Math.floor(finalstring/2); i++ ) { if (finalstring[i] !== finalstring[len - 1 - i]) { return false; } } return true; } console.log(isPalindrome("madam i'm adam"));

Is there anything wrong with my code? Point to be noted, I'm not allowed to use any built-in or library function. Your help would be much appreciable. Tnx.

Answer1:

Within the "for" clause, replace "Math.floor(finalstring/2)" by "Math.floor(len/2)"

Also, you might want to improve your punctuation removal so that it also removes apostrophes, for instance.

So your code would become the following:

function isPalindrome (str) { var nopunctuation = str.replace(/\W/g,""); var nospaces = nopunctuation.replace(/\s/g,""); var finalstring = nospaces; var len = finalstring.length; for ( var i = 0; i < Math.floor(len/2); i++ ) { if (finalstring[i] !== finalstring[len - 1 - i]) { return false; } } return true; }

Recommend

  • Why is my C code skipping the next user input?
  • Find the largest palindrome made from the product of two 3-digit numbers - Javascript
  • how to replace the null character [closed]
  • How to replace exact keyword in a sentence in UITextView
  • Reversing a string with strrev - C
  • Javascript Palindrome Logic
  • Java : How to tint this PNG programmatically?
  • HttpListener.IsSupported is false on XP SP3
  • Parse a date string in a specific locale (not timezone!)
  • jQuery ready not fired after rails link_to is clicked
  • Java: can you cast Class into a specific interface?
  • How to get a value (ex: baseURL) in every Karate feature?
  • AES padding and writing the ciphertext to a disk file
  • How would I use PHP exceptions to define a redirect?
  • angularjs unit test when to use $rootScope.$new()
  • How to set/get protobuf's extension field in Go?
  • Submit form in a displaytag pagination
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • How to handle AllServersUnavailable Exception
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Rearranging Cells in UITableView Bug & Saving Changes
  • 0x202A in filename: Why?
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • How to delete a row from a dynamic generate table using jquery?
  • Acquiring multiple attributes from .xml file in c#
  • using HTMLImports.whenReady not working in chrome
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • EntityFramework adding new object to nested object collection
  • Binding checkboxes to object values in AngularJs
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?