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 to the right: ​
​  leftIndex++;
​  ​ // Move rightIndex one to the left: ​
​  rightIndex--;
​  }
​  
​  ​ // If we got through the entire loop without finding any ​
​  ​ // mismatches, the string must be a palindrome: ​
​  ​ return ​ ​ true ​;
​  }

Let’s determine the Big O of this algorithm.

In this case, N is the size of the string passed to this function.

The guts of the algorithm takes place within the while loop. Now, this loop is somewhat interesting because it only runs until it reaches the midpoint of the string. That would mean that the loop runs N / 2 steps.

However, Big O ignores constants. Because of this, we drop the division by 2, and our algorithm is O(N).