Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions maths/is_palindrome.ts
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/fatima-0000>
*/

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];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why build a reverse string using repeated string concatenations when you could instead just check character-wise equality right in this loop?

}
return stringValue == reverseString;
};
10 changes: 10 additions & 0 deletions maths/test/is_palindrome.test.ts
Original file line number Diff line number Diff line change
@@ -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",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double "for"

(nums, expected) => {
expect(IsPalindrome(nums)).toBe(expected);
},
);
});