diff --git a/DIRECTORY.md b/DIRECTORY.md index b96d789d..aeff71c7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -27,6 +27,7 @@ * [Is Even](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_even.ts) * [Is Leap Year](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_leap_year.ts) * [Is Odd](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_odd.ts) + * [Is Palindrome](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_palindrome.ts) * [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/lowest_common_multiple.ts) * [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_square.ts) * [Pronic Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pronic_number.ts) diff --git a/maths/is_palindrome.ts b/maths/is_palindrome.ts new file mode 100644 index 00000000..3533c2b2 --- /dev/null +++ b/maths/is_palindrome.ts @@ -0,0 +1,18 @@ +/** + * A function to see if a number is a Pallindrome + * @param number The input integer + * @return {boolean} True or False based on the integer + * @example isPalindrome(12321) => true | isPalindrome(455) => false + * @see https://en.wikipedia.org/wiki/Palindromic_number + * @author FatimaChariwala + */ + +export const IsPalindrome = (number: number): boolean => { + let reverseString = ""; + let stringValue = number.toString(); + let length = stringValue.length - 1; + for (let i = length; i >= 0; --i) { + reverseString = reverseString + stringValue[i]; + } + return stringValue == reverseString; +}; \ No newline at end of file diff --git a/maths/test/is_palindrome.test.ts b/maths/test/is_palindrome.test.ts new file mode 100644 index 00000000..af7122f2 --- /dev/null +++ b/maths/test/is_palindrome.test.ts @@ -0,0 +1,10 @@ +import { IsPalindrome } from "../is_palindrome"; + +describe("IsPalindrome", () => { + test.each([[5, true], [1234, false], [12321, true], [32.23, true], [31343, false]])( + "correct output for for %i", + (nums, expected) => { + expect(IsPalindrome(nums)).toBe(expected); + }, + ); +}); \ No newline at end of file