-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.ts
45 lines (35 loc) · 1 KB
/
palindrome.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* Question: write a fn that takes a string and returns true if it's a palindrome,
and false otherwise */
function isPalindrome(str: string): boolean {
if (!str) return false;
let left = 0, right = str.length - 1;
while (left <= right) {
if (str[left] !== str[right]) return false;
left++;
right--;
}
return true;
}
/* Tests */
import { assertEquals } from "./deps.ts";
Deno.test("isPalindrome('')", () => {
assertEquals(isPalindrome(""), false);
});
Deno.test("isPalindrome('a')", () => {
assertEquals(isPalindrome("a"), true);
});
Deno.test("isPalindrome('ab')", () => {
assertEquals(isPalindrome("ab"), false);
});
Deno.test("isPalindrome('aba')", () => {
assertEquals(isPalindrome("aba"), true);
});
Deno.test("isPalindrome('acbca')", () => {
assertEquals(isPalindrome("acbca"), true);
});
Deno.test("isPalindrome('acbcaa')", () => {
assertEquals(isPalindrome("acbcaa"), false);
});
Deno.test("isPalindrome('xahuisb')", () => {
assertEquals(isPalindrome("xahuisb"), false);
});