August 2024
Intermediate to advanced
516 pages
11h 47m
English
A palindrome is a word or phrase that reads the same both forward and backward. Some examples include racecar, kayak, and deified.
Here’s a function that determines whether a string is a palindrome:
| | function isPalindrome(string) { |
| | let leftIndex = 0; |
| | let rightIndex = string.length - 1; |
| | |
| | // Iterate until leftIndex reaches the middle of the array: |
| | while (leftIndex < Math.floor(string.length / 2)) { |
| | // If the character on the left doesn't equal the character |
| | // on the right, the string is not a palindrome: |
| | if (string[leftIndex] !== string[rightIndex]) { |
| | return false; |
| | } |
| | |
| | leftIndex += 1; |
| | rightIndex -= 1; |
| | } |
| | |
| | // If we got through the entire loop without finding any |
| | // mismatches, ... |
Read now
Unlock full access