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
Binary file added .gitignore
Binary file not shown.
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.

27 changes: 27 additions & 0 deletions src/utils/palindrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Author: Amanda Ruff
* Date: 2 June 2026
* File: palindrome.js
* Description: This script checks if a string is a palindrome.
*/
'use strict';

// The isPalindrome function checks whether a string reads the same forward and backward
function isPalindrome(str) {

// If the input is not a string, throw an error
if (typeof str !== 'string') {
throw new Error('Input must be a string');
}

// Remove spaces, convert to lowercase, and remove non-alphanumeric characters
const cleanedString = str.toLowerCase().replace(/[^a-z0-9]/g, '');

// Reverse the cleaned string
const reversedString = cleanedString.split('').reverse().join('');

// Return true if the cleaned string matches the reversed string
return cleanedString === reversedString;
}

module.exports = { isPalindrome }; // Export the isPalindrome function for use in other scripts
30 changes: 30 additions & 0 deletions test/utils/palindrome.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Author: Amanda Ruff
* Date: 2 June 2026
* File: palindrome.spec.js
* Description: This script tests the isPalindrome function.
*/
'use strict';

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

// The describe() function is a test suite that contains one or more tests
describe('palindrome.js', () => {

// The it() function is a test spec that checks a valid palindrome
it('should return true when the string is a palindrome', () => {
const result = isPalindrome('racecar'); // Call the function with a palindrome
expect(result).toBe(true); // The expected result is true
});

// The it() function is a test spec that checks a non-palindrome
it('should return false when the string is not a palindrome', () => {
const result = isPalindrome('hello'); // Call the function with a non-palindrome
expect(result).toBe(false); // The expected result is false
});

// The it() function is a test spec that checks invalid input
it('should throw an error when the input is not a string', () => {
expect(() => isPalindrome(123)).toThrow('Input must be a string');
});
});