Valid Palindrome Using Every Method
In this new solution, I began with saving the same string variable that I used in my previous solution, but I chained an additional method, the split method, onto the end to split the string into an array, since every is an array method. This variable is used as the returned value while using the every method in the same line.
The every method takes in a function as its first argument. This function is used as a test for every element in the array that the method is stringed onto, if every element passes the test, then the method returns true, but if even a single element fails the test, the method returns false. Since this coding challenge’s instructions tell you to check if the string is the same when it’s reversed, the every method can be used to check if the last element is the same as the first, then if the second-to-last is the same as the second, and so on.
In the callback function used inside every, the first argument is the element of the array that’s currently being used in the every loop. In this specific instance of the method, I want to use the second argument in the callback function, which will allow me to access the index while inside of the function body. I want to check if each character is equal to the element on the opposite side of the string. To find the element at the end, I can use string.length — 1 to get the end, then use the index of each character being checked to move that end character one element closer to the front as the method runs.