Coding challenge Day-3

Coding challenge Day-3

Third day of I4G coding challenge

Palindrome Number

Question

Used JavaScript for the challenge

Today's challenge was a bit more fun, and I had a clear idea immediately after I saw the question. This was the fastest I have solved since the start of the challenge.

Thought process

The following was my thought process after reading the question and understanding it:

  1. Convert the integer to a string.

  2. Reverse the string

  3. Use different string and array methods

  4. Convert it back to an integer.

  5. Compare the original input value to the reversed value.

  6. If they match, return true, else, return false.

Implementation

These were the steps followed to achieve the implementation:

  1. Created a variable reversedX which held the result of the reversed integer.

  2. Applied ""+n method which converted the input integer (x) to a string.

  3. Used the split() method which splitted the string to individual elements.

  4. Used the reverse() method.

  5. Used the join() method to join the element back to a string.

  6. Used the parseInt() method to convert the string back to integer. This step was taken to make it possible to compare using the strict equality operator ===

  7. Used an if block to check if the input integer (x) was equal to the reversed integer (reversedX).

  8. If equal, the block returned true, else, it returned false.

What I learnt

Learnt about another method I can use to convert integers to strings which was the ""+n method

Conclusion

Answer

Generally, I had more fun doing today's challenge. My code passed about 11510 test cases in 331ms using 51.3MB memory space

Thank you for reading through!

Day three(3) down, and Seven more days to go!

You can follow me on Twitter