Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bc370ed

Browse files
Implement two possible solutions to check if a gven string is palindrome or not, write tests
1 parent 01310fc commit bc370ed

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

‎02_palindrome/index.js

Lines changed: 14 additions & 0 deletions
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

Lines changed: 34 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /