DEV Community

Achilonu Chinwendu Faustina
Achilonu Chinwendu Faustina

Posted on

DAY 3 CODE CHALLENGE (PALINDROME INTEGER)

PALINDROME INTEGER

In our challenge today, we were asked to identify if a given integer is Palindrome or not. To solve any challenge, one most be willing to understand the question before execution. Let's move to the question of the day.

QUESTION AND EXPLANATION
Given an integer x, return true if x is palindrome integer.

One might wonder what a palindrome integer is but wonder not.
An integer is said to be palindrome when it reads the same both forward and backward. For example, 242 is palindrome while 148 is not. Let us also look at the following examples below:

Example 1
input: x = 242
output: true
Here output is true because from right to left also reads 242.

Example 2
input: x = -242
output: false
Reason: From left to right reads -242 while from right to left reads 242-, making it non palindrome.

Example 3
input: x = 10
output: false
This is because from right to left reads 01, which implies it not palindrome.

PROCEDURE

-Set backward to input x, convert to string, split and reverse it, then join as illustrated below.

let backward = x.toString().split('').reverse().join('')

  • Compare the backward string to input x and return as shown below.

return(x.string() === backward)

RESULT OUTPUT
Image description

Top comments (0)