|
| 1 | +/*************************************************************************************** |
| 2 | +* * |
| 3 | +* CODERBYTE BEGINNER CHALLENGE * |
| 4 | +* * |
| 5 | +* Palindrome * |
| 6 | +* Using the JavaScript language, have the function Palindrome(str) take the str * |
| 7 | +* parameter being passed and return the string true if the parameter is a palindrome, * |
| 8 | +* (the string is the same forward as it is backward) otherwise return the string * |
| 9 | +* false. For example: "racecar" is also "racecar" backwards. Punctuation and numbers * |
| 10 | +* will not be part of the string. * |
| 11 | +* * |
| 12 | +* SOLUTION * |
| 13 | +* I have two outliers. The first is to remove all spaces between words since it can * |
| 14 | +* cause the reverse string to be wrong. The second is to convert string to lower case * |
| 15 | +* so that any capitalized words cause a false answer. I am going to first remove all * |
| 16 | +* spaces from the string. Then I am going to convert a new string that converts the * |
| 17 | +* modified str into an array and then reverses the contents and then converts it back * |
| 18 | +* to a string. I will compare the modified string and the newStr to see if they are * |
| 19 | +* equal signifying I have a Palindrome. * |
| 20 | +* * |
| 21 | +* Steps for solution * |
| 22 | +* 1) Modify str by removing all spaces and converting to lower case. * |
| 23 | +* 2) Create a new string by converting modified str into an array, reverse it and * |
| 24 | +* then convert it back to a string * |
| 25 | +* 3) Compare two strings to see if equal meaing I have a Palindrome * |
| 26 | +* * |
| 27 | +***************************************************************************************/ |
| 28 | + |
| 29 | +function Palindrome(str) { |
| 30 | + |
| 31 | + str = str.replace(/ /g,"").toLowerCase(); |
| 32 | + var compareStr = str.split("").reverse().join(""); |
| 33 | + |
| 34 | + if (compareStr === str) { |
| 35 | + return true; |
| 36 | + } |
| 37 | + else { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | +} |
0 commit comments