Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added js solution (and tests) for challenge palindrome-number #87

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
30 changes: 30 additions & 0 deletions challenges/easy/palindrome-number/solutions/aleattene/solution.js
Original file line number Diff line number Diff line change
@@ -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,
}
18 changes: 18 additions & 0 deletions challenges/easy/palindrome-number/solutions/aleattene/test.js
Original file line number Diff line number Diff line change
@@ -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);
});