Jump to content

Please Help Me Make Sense Of This Question


Guest markv
 Share

Recommended Posts

I'm trying to do an assignment from a book I borrowed. I have to "Create a script with a function isPalindrome(x) which returns true if and only if x is a string that is a palindrome, that is, it reads the same forward an backwards: A man, a plan, a canal, Panama!"Does this make sense to anybody?

Link to comment
Share on other sites

In order to do that, first you'll need to remove any extra characters from the input string. That will include spaces, punctuation, etc. You can do that using the string.replace method. Once the string is just letters, you need to transform it to lowercase or uppercase, either one. You can use string.toLowerCase or string.toUpperCase to do that. Once that's done, you need to loop through it and compare the characters. So you have a normal loop where you go through and look at each character, and you need to figure out which other character to compare it with. So if you're looking at the first character, you need to compare it with the last. If you're looking at the second character, you need to compare it with the second-to-last character, and so on. So if i is the position of the character you're on, and len is the length of the string, then len - i is the position of the character to compare with. You can use string.charAt to get a certain character in the string by position. If any of the characters don't match then you immediately return false, if any match fails you know it's not a palindrome.Here's the Javascript string object reference:http://www.w3schools.com/jsref/jsref_obj_string.aspYou could also theoretically just reverse the string and compare the two instead of going character-by-character, but Javascript doesn't have a native string reverse function so you'd still have to write that yourself.

Link to comment
Share on other sites

It's like one of the code-golf questions that kick around on the web: do such-and-such in as little code as possible. Somet times people come up with stupendous code that is dense as last year's christmas pudding.

Link to comment
Share on other sites

In order to do that, first you'll need to remove any extra characters from the input string. That will include spaces, punctuation, etc. You can do that using the string.replace method. Once the string is just letters, you need to transform it to lowercase or uppercase, either one. You can use string.toLowerCase or string.toUpperCase to do that. Once that's done, you need to loop through it and compare the characters. So you have a normal loop where you go through and look at each character, and you need to figure out which other character to compare it with. So if you're looking at the first character, you need to compare it with the last. If you're looking at the second character, you need to compare it with the second-to-last character, and so on. So if i is the position of the character you're on, and len is the length of the string, then len - i is the position of the character to compare with. You can use string.charAt to get a certain character in the string by position. If any of the characters don't match then you immediately return false, if any match fails you know it's not a palindrome.Here's the Javascript string object reference:http://www.w3schools.com/jsref/jsref_obj_string.aspYou could also theoretically just reverse the string and compare the two instead of going character-by-character, but Javascript doesn't have a native string reverse function so you'd still have to write that yourself.
that doesn't leave much up to the imagination. took all the fun out of it, lol.
Link to comment
Share on other sites

He sounded like he needed some help...For extra credit, make the character comparison loop quit once it's processed half of the string. Once it compares the first half to the second half it doesn't need to recompare the second half back to the first half.

Link to comment
Share on other sites

It takes a special class of geek to have, memorized, a favourite palindrome :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...