From 7139c1411a899e5a2d0d08b65160c65ddd6b9516 Mon Sep 17 00:00:00 2001 From: Alessandro Attene <74595044+aleattene@users.noreply.github.com> Date: Wed, 17 Nov 2021 12:45:09 +0100 Subject: [PATCH 1/2] feat: added js solution (and tests) for challenge palindrome-number --- .../solutions/aleattene/solution.js | 32 +++++++++++++++++++ .../solutions/aleattene/test.js | 18 +++++++++++ 2 files changed, 50 insertions(+) create mode 100644 challenges/easy/palindrome-number/solutions/aleattene/solution.js create mode 100644 challenges/easy/palindrome-number/solutions/aleattene/test.js 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..330057f --- /dev/null +++ b/challenges/easy/palindrome-number/solutions/aleattene/solution.js @@ -0,0 +1,32 @@ +/* +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 From abea037f98a7e275e886bb70f2eb96720d8d38bf Mon Sep 17 00:00:00 2001 From: Alessandro Attene <74595044+aleattene@users.noreply.github.com> Date: Wed, 17 Nov 2021 13:02:53 +0100 Subject: [PATCH 2/2] style: improved the code layout --- .../easy/palindrome-number/solutions/aleattene/solution.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/challenges/easy/palindrome-number/solutions/aleattene/solution.js b/challenges/easy/palindrome-number/solutions/aleattene/solution.js index 330057f..9e0794c 100644 --- a/challenges/easy/palindrome-number/solutions/aleattene/solution.js +++ b/challenges/easy/palindrome-number/solutions/aleattene/solution.js @@ -5,13 +5,11 @@ To test the solution, type from CLI: npm test (required node.js and jest framewo function isPalindrome(number) { - // From number to String + // 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]) {