Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.DS_Store
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/utils/palindrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Author: Chris Newell
* Date: 18 January 2026
* File: palindrome.js
* Description: This script checks whether a string is a palindrome
*/
'use strict';

// The isPalindrome function checks if a string reads the same forward and backward
function isPalindrome(str) {
if (typeof str !== 'string') {
throw new Error('Input must be a string');
}

// Normalize: lowercase and remove non-alphanumeric characters
const normalized = str.toLowerCase().replace(/[^a-z0-9]/g, '');
const reversed = normalized.split('').reverse().join('');

return normalized === reversed;
}

module.exports = { isPalindrome };
23 changes: 23 additions & 0 deletions test/utils/palindrome.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Author: Chris Newell
* Date: 18 January 2026
* File: palindrome.spec.js
* Description: This script tests the isPalindrome function.
*/
'use strict';

const { isPalindrome } = require('../../src/utils/palindrome');

describe('palindrome.js', () => {
it('should return true for a simple palindrome', () => {
expect(isPalindrome('racecar')).toBe(true);
});

it('should ignore case and non-alphanumeric characters', () => {
expect(isPalindrome('A man, a plan, a canal: Panama')).toBe(true);
});

it('should return false when the string is not a palindrome', () => {
expect(isPalindrome('hello')).toBe(false);
});
});