My code:
function palindrome(str) {
return str.split('').every((char, i) => {
return char === str[str.length - i - 1] ? true : false;
})
}
palindrome('abcba');
In using the debugger and console.log I get why in the comparison statement we write - i
, because that gets us to the correct index on the opposite side but how would one know to do this from the very beginning?
Is there an explanation or a diagram of some sort that explains how or why - i
will get you to the index of the character on the opposite side? Just finding it difficult to properly wrap my had around this one.
Any help much appreciated although I imagine this is a difficult one to explain.
Okay let's break it down.
str.split('')
This line breaks your 'abcba'
string into an array. The result will be:
['a', 'b', 'c', 'b', 'a']
Now that you have an array that you can loop over and select certain indexes from the array. This is very useful in the case of a palindrome.
Because you want to know if the word is the same when it is mirrored. And you figure that out by starting at the head and tail of the array.
The first item in the array being the index of 0 [0]
and the last one, well that depends on the array. But if you have the array and and subtract [array.length - 1]
, which in the word abcba is [5 - 1]
and you end up with the last item in the array, which has the index of 4 [4]
.
// First iteration.
// First and last items in array.
// [0] and [array.length - 1] or [4].
↓ ↓
['a', 'b', 'c', 'b', 'a']
So in the loop you go from left to right in your array. And compare the first value with the last value. Your example uses the every
method which at the end returns true if every condition in your loop has returned true, or false whenever that is not the case.
The loop also exposes a current index value. In your example called i
, short for index. If you finished comparing the first and last value you want to shift over to the next item in the array to start comparing the second and first to last value. That's where the formula comes from.
str.split('').every((char, i) => {
return char === str[str.length - i - 1] ? true : false;
});
In the first iteration the value of i
is 0. So you have the formula: length of the array, subtract 0 and subtract 1
. Which gives you the last item.
In the second iteration i
is now 1. Meaning we are in the second item of the array. If you apply i
to the formula again, you can start to count back from the end of the array: length of the array, subtract 1 and subtract 1
. Which adds up to 3
.
// Second iteration.
// Second and first to last item in array.
// [1] and [array.length - 2] or [3].
↓ ↓
['a', 'b', 'c', 'b', 'a']
And with the next iteration you get an overlap, but not in cases with an even numbered word. But here they are both checking if c
matches c
.
// Third iteration.
// Third and and second to last item in array.
// [2] and [array.length - 3] or [2].
↓
↓
['a', 'b', 'c', 'b', 'a']
And this continues on until the last item in the array is reached with the loop continuing counting up, and the formula counting down.
I hope this makes sense and helps you understand it a bit more.