diff --git a/challenges/easy/palindrome-number/solutions/aleattene/solution.js b/challenges/easy/palindrome-number/solutions/aleattene/solution.js new file mode 100644 index 0000000..9e0794c --- /dev/null +++ b/challenges/easy/palindrome-number/solutions/aleattene/solution.js @@ -0,0 +1,30 @@ +/* +JS solution for challenge: "Palindrome Number" +To test the solution, type from CLI: npm test (required node.js and jest framework) +*/ + + +function isPalindrome(number) { + // From number to string + let word = number.toString() + // Shift indexes (left and right) + let i = 0; + let j = word.length - 1; + // Returns false as soon as two different values are found + while (i <= j) { + if (word[i] !== word[j]) { + // The number is not palindrome + return false; + } + i++; j--; + } + // The number is palindrome + return true; +} + + + +// Exports for the tests +module.exports = { + isPalindrome, +} diff --git a/challenges/easy/palindrome-number/solutions/aleattene/test.js b/challenges/easy/palindrome-number/solutions/aleattene/test.js new file mode 100644 index 0000000..693932b --- /dev/null +++ b/challenges/easy/palindrome-number/solutions/aleattene/test.js @@ -0,0 +1,18 @@ +/* +To start the tests, type from CLI: npm test (required node.js and jest framework) +*/ + +// Import Functions +const {isPalindrome} = require("./solution.js"); + +// Tests for isPalindrome Function +test('Palindrome Number - Unit Tests', () => { + expect(isPalindrome(121)).toBe(true); + expect(isPalindrome(-121)).toBe(false); + expect(isPalindrome(10)).toBe(false); + expect(isPalindrome(-101)).toBe(false); + expect(isPalindrome(12321)).toBe(true); + expect(isPalindrome(123456)).toBe(false); + expect(isPalindrome(0)).toBe(true); + expect(isPalindrome(1111111111111111)).toBe(true); +}); \ No newline at end of file