 # Palindrome and Integer Reversal Test

This post is part of my Rise to the Equation series on data structures and algorithms.

The Problem

Given a string, return true if the string is a palindrome or false if it is not.

Palindromes are strings that form the same word if it is reversed. Do include spaces and punctuation in determining if the string is a palindrome.

Examples:

palindrome(“abba”) === true

palindrome(“abcdefg”) === false

I admit I was a bit lost on how to approach this problem. I knew the return function would be a comparison of the regular string and the reversed string, but that was about it. After a little research on arrays, I learned that there was a method called .reverse() that would do exactly what I needed. However, I needed to turn the string into an array first in order to use that method.

Here are two examples of solutions for how to solve this challenge:

Solution #1: The Classic

```function palindrome(str) {
let reversed = str.split('').reverse().join('')
return str === reversed
}
```

Using split, reverse, and join is the answer you will most commonly find when researching this problem. Split turns the string into an array with a comma separating each character. Reverse then reverses the array. And then join transforms the array back into a string. In the code above, you can see that I set to a variable the result of those actions. Since I need to return either true or false from the function, I can do a direct comparison of the string I passed in (called str) to the string I reversed (called reversed).

Solution #2: The Deep Cut

```function reverse(str) {

return str.split('').reduce((reversedArray, character) => {
// the following function executes for every element in our array
return character + reversedArray
}, '')
}
```

This one starts off the same as our first solution by using split, but then it diverges. Here, we make use of a helper method called reduce. Like cookie dough, reduce takes a variety of “ingredients” which are then mixed/smashed together until solid dough is formed. The ingredients for reduce are the following four arguments:

• Accumulator – the value that is returned from this function (i.e., our reversedArray)
• Current Value – the specific character of the string we are looping over.
• Current Index – an empty string ()
• Source Array – our str array that we applied our split helper to

So, how would our approach change if we wanted to reverse the order of a series of integers (but keep negative numbers)?

Solution #1: Good Ol’ If Statement

```function reverseInt(n) {

let reverseNum = parseInt(n.toString().split('').reverse().join(''))

// handle negative numbers
if (x < 0) {
return reverseNum * -1
}
return reverseNum
}
```

Like our string reversal solution in the previous section, we will use the split/reverse/join sequence. However, those helper methods only work on strings so we’ll need to first use toString() to convert our integer(s) into strings. After that, we turn everything back into integers with parseInt().

If we run just that block of code with the integer -15, we would return 51-. That’s not quite what we’re looking for, so we add a second if statement that checks if we need to add a negative sign.

Solution #2: Cleaner and Math-y

```function reverseInt(n) {
let reverseNum = parseInt(n.toString().split('').reverse().join(''))
return reverseNum * Math.sign(n)
}
```

The first part of this function looks the same, but instead of using an if statement we call upon the built-in Math helper called sign. If n is positive, Math.sign() will return 1. If it is negative, it will return -1. And if it is 0, it will return 0. That being the case, we can just multiply reverseNum by the result of passing n into Math.sign().

And that’s it!

Like our palindrome problem at the top of this post, either solution would work as an answer to a code challenge. There are multiple ways to solve the same problem in these situations so don’t worry if you came up with something else. Javascript is very flexible in that way, which is either a delight or the source of frustration — sometimes both — depending on who you ask!

I hope you found this breakdown interesting. I’ll post another one soon. 🙂

Mozilla Development Network Resources