Skip to content

Commit 6422cb8

Browse files
Merge pull request #5 from somekindofwallflower/feature/palindrome
Implement two possible solutions to check if a gven string is palindr…
2 parents 01310fc + bc370ed commit 6422cb8

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

02_palindrome/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
// Solution 1
3+
// Reverse the string and store the result in an variable than compare string to the reversed value
4+
// function palindrome(str) {
5+
// // const reverseStr = str.split("").reduce((rev, char) => char + rev, "");
6+
// const reverseStr = str.split("").reverse().join("");
7+
// return reverseStr === str;
8+
// }
9+
10+
// Solution 2
11+
function palindrome(str) {
12+
return str.split('').every((char, i, arr) => char === arr[arr.length - i - 1])
13+
}
14+
module.exports = palindrome;

02_palindrome/test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const palindrome = require('./index');
2+
3+
test("palindrome function is defined", () => {
4+
expect(typeof palindrome).toEqual("function");
5+
})
6+
7+
8+
test("'aba' is palindrome", () => {
9+
expect(palindrome("aba")).toBeTruthy();
10+
})
11+
12+
test("' aba' is not palindrome", () => {
13+
expect(palindrome(" aba")).toBeFalsy();
14+
})
15+
16+
test("'aba ' is not palindrome", () => {
17+
expect(palindrome("aba ")).toBeFalsy();
18+
})
19+
20+
test('"greetings" is not a palindrome', () => {
21+
expect(palindrome('greetings')).toBeFalsy();
22+
});
23+
24+
test('"1000000001" a palindrome', () => {
25+
expect(palindrome('1000000001')).toBeTruthy();
26+
});
27+
28+
test('"Fish hsif" is not a palindrome', () => {
29+
expect(palindrome('Fish hsif')).toBeFalsy();
30+
});
31+
32+
test('"pennep" a palindrome', () => {
33+
expect(palindrome('pennep')).toBeTruthy();
34+
});

0 commit comments

Comments
 (0)