From bcd4f0ec1fdbc67c88a1c3142e3aba54619a5055 Mon Sep 17 00:00:00 2001 From: Nicholas Skelton Date: Wed, 3 Jun 2026 23:20:14 -0500 Subject: [PATCH] Added check_if_palindrome function --- src/utils/check_if_palindrome.js | 18 +++++++++++++++ test/utils/check_if_palindrome.spec.js | 32 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/utils/check_if_palindrome.js create mode 100644 test/utils/check_if_palindrome.spec.js diff --git a/src/utils/check_if_palindrome.js b/src/utils/check_if_palindrome.js new file mode 100644 index 0000000..1877f7b --- /dev/null +++ b/src/utils/check_if_palindrome.js @@ -0,0 +1,18 @@ +/** + * Author: Nicholas Skelton + * Date: 3 June 2026 + * File: check_if_palindrome.js + * Description: This script checks to see if a string is a palindrome + */ +'use strict'; + +function isPalindrome(str) { + // Convert string to lowercase and remove all non-alphanumeric characters + const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, ''); + // Splits string into an array of individual characters, reverses it, and rejoins the string + const reversed = cleaned.split('').reverse().join(''); + // Compares the cleaned string to the reversed + return cleaned === reversed; +} + +module.exports = { isPalindrome }; // Export the factorial function for use in other scripts \ No newline at end of file diff --git a/test/utils/check_if_palindrome.spec.js b/test/utils/check_if_palindrome.spec.js new file mode 100644 index 0000000..c5afb98 --- /dev/null +++ b/test/utils/check_if_palindrome.spec.js @@ -0,0 +1,32 @@ +/** + * Author: Nicholas Skelton + * Date: 3 June 2026 + * File: check_if_palindrome.spec.js + * Description: This script tests the isPalindrome function. + */ +'use strict'; + +const { isPalindrome } = require('../../src/utils/check_if_palindrome'); // Import the isPalindrome function from the palindrome.js file + +// The describe() function is a test suite that contains one or more tests +describe('palindrome.js', () => { + + // The it() function is a test spec that contains one or more expectations + it('should return true for a valid palindrome', () => { + const result = isPalindrome('racecar'); // Call the isPalindrome function with the value of racecar + expect(result).toBe(true); // The expected result is true + }); + + // The it() function is a test spec that contains one or more expectations + it('should return false for a non-palindrome string', () => { + const result = isPalindrome('hello'); // Call the isPalindrome function with the value of hello + expect(result).toBe(false); // The expected result is false + }); + + // The it() function is a test spec that contains one or more expectations + it('should ignore spaces, punctuation, and capitalization', () => { + const result = isPalindrome('A man, a plan, a canal: Panama'); // Call the isPalindrome function with a formatted palindrome + expect(result).toBe(true); // The expected result is true + }); + +}); \ No newline at end of file