August 2020
Intermediate to advanced
508 pages
11h 53m
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 JavaScript function that determines whether a string is a palindrome:
| | function isPalindrome(string) { |
| | |
| | // Start the leftIndex at index 0: |
| | let leftIndex = 0; |
| | // Start rightIndex at last index of array: |
| | let rightIndex = string.length - 1; |
| | |
| | // Iterate until leftIndex reaches the middle of the array: |
| | while (leftIndex < 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; |
| | } |
| | |
| | // Move leftIndex one ... |
Read now
Unlock full access