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
18 changes: 18 additions & 0 deletions src/utils/check_if_palindrome.js
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions test/utils/check_if_palindrome.spec.js
Original file line number Diff line number Diff line change
@@ -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
});

});