Bonfire: Check for Palindromes Difficulty: Level 1

Return true if the given string is a palindrome. Otherwise, return false.

 palindrome is a word or sentence that’s spelled the same way both forward and backward, ignoring punctuation, case, and spacing.

You’ll need to remove punctuation and turn everything lower case in order to check for palindromes.

We’ll pass strings with varying formats, such as “racecar”, “RaceCar”, and “race CAR” among others.

Here are some helpful links:

 

My solution:

function palindrome(str) {

//delete global spaces
var nospace = str.replace(/\s+/g,”);
var nospace = str.replace(/\W+/g,”);

//replace nospace with lowercase letters
var newstring = nospace.replace(/[A-Za-z]g,/).toLowerCase();

//reverse the string without spaces
revstr = nospace.split(”).reverse().join(”).toLowerCase();

//test original string w/o spaces to reversed string w/o //spaces
if (newstring === revstr)
{
return true;
}
else {
return false;
}
}

palindrome(“A man, a plan, a canal. Panama”);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s