diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..27592c8 Binary files /dev/null and b/.gitignore differ diff --git a/package-lock.json b/package-lock.json index 8fce75c..8e2718d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,7 @@ "": { "name": "nodejs-practice-repository", "version": "1.0.0", - "license": "ISC", + "license": "MIT", "devDependencies": { "jest": "^29.7.0" } diff --git a/src/utils/palindrome.js b/src/utils/palindrome.js new file mode 100644 index 0000000..f9d81f1 --- /dev/null +++ b/src/utils/palindrome.js @@ -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 \ No newline at end of file diff --git a/test/utils/palindrome.spec.js b/test/utils/palindrome.spec.js new file mode 100644 index 0000000..4727293 --- /dev/null +++ b/test/utils/palindrome.spec.js @@ -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'); + }); +}); \ No newline at end of file