Code/README fixes for the "Palindrome Check".

This commit is contained in:
Oleksii Trekhleb
2022-01-23 21:47:57 +01:00
parent ea28788ed8
commit 90addf9b18
6 changed files with 48 additions and 41 deletions

View File

@@ -1,25 +0,0 @@
# Palindrome Check
A Palindrome is a string that reads the same forwards and backwards.
This means that the second half of the string is the reverse of the
first half.
## Examples
The following are palindromes (thus would return TRUE):
- "a"
- "pop" -> p + o + p
- "deed" -> de + ed
- "kayak" -> ka + y + ak
- "racecar" -> rac + e + car
The following are NOT palindromes (thus would return FALSE):
- "rad"
- "dodo"
- "polo"
## References
[GeeksforGeeks - Check if a number is Palindrome](https://www.geeksforgeeks.org/check-if-a-number-is-palindrome/)

View File

@@ -1,14 +0,0 @@
import palindromeCheck from '../palindromeCheck';
describe('palindromeCheck', () => {
it('should return whether or not the string is a palindrome', () => {
expect(palindromeCheck('a')).toBe(true);
expect(palindromeCheck('pop')).toBe(true);
expect(palindromeCheck('deed')).toBe(true);
expect(palindromeCheck('kayak')).toBe(true);
expect(palindromeCheck('racecar')).toBe(true);
expect(palindromeCheck('rad')).toBe(false);
expect(palindromeCheck('dodo')).toBe(false);
expect(palindromeCheck('polo')).toBe(false);
});
});

View File

@@ -0,0 +1,29 @@
# Palindrome Check
A [Palindrome](https://en.wikipedia.org/wiki/Palindrome) is a string that reads the same forwards and backwards.
This means that the second half of the string is the reverse of the
first half.
## Examples
The following are palindromes (thus would return `TRUE`):
```
- "a"
- "pop" -> p + o + p
- "deed" -> de + ed
- "kayak" -> ka + y + ak
- "racecar" -> rac + e + car
```
The following are NOT palindromes (thus would return `FALSE`):
```
- "rad"
- "dodo"
- "polo"
```
## References
- [GeeksForGeeks - Check if a number is Palindrome](https://www.geeksforgeeks.org/check-if-a-number-is-palindrome/)

View File

@@ -0,0 +1,15 @@
import isPalindrome from '../isPalindrome';
describe('palindromeCheck', () => {
it('should return whether or not the string is a palindrome', () => {
expect(isPalindrome('a')).toBe(true);
expect(isPalindrome('pop')).toBe(true);
expect(isPalindrome('deed')).toBe(true);
expect(isPalindrome('kayak')).toBe(true);
expect(isPalindrome('racecar')).toBe(true);
expect(isPalindrome('rad')).toBe(false);
expect(isPalindrome('dodo')).toBe(false);
expect(isPalindrome('polo')).toBe(false);
});
});

View File

@@ -3,9 +3,10 @@
* @return {boolean}
*/
export default function palindromeCheck(string) {
export default function isPalindrome(string) {
let left = 0;
let right = string.length - 1;
while (left < right) {
if (string[left] !== string[right]) {
return false;
@@ -13,5 +14,6 @@ export default function palindromeCheck(string) {
left += 1;
right -= 1;
}
return true;
}