Sorry, the last two lines were just there from when I was running the tests; I didn’t. mean to copy them in. I had st1 and str2 both initialized with values above my function when I was testing, so those bottom lines were working fine.
I checked the value of charsFromEnd before I posted here, by moving the line it’s declared on outside of the function, and it’s returning the expected value. That’s why I don’t understand why it’s not working.
From what I understand, the 2nd param in the includes() method (fromIndex) dictates the index it starts checking from, and if that number is a negative number, then it will count back from the end of the string.
So, for example, in test 5, which is checking for “n” in “Connor”, it should only be looking at the last letter in “Connor” (starting at index -1). charsFromEnd does have a value of -1 in this case, but it seems to be checking the whole word.
Yet, in test 6, it’s checking for “specification” in “Walking on water and developing software from a specification are easy if both are frozen”, it’s checking from index -13, and somehow returning true?
I had mistakenly been referencing the information on MDN for the array prototype, instead of string, for includes(), which is where I got the information that the second parameter counts back from the end if it’s a negative integer.
I did read through the MDN and W3 docs before posting here, and didn’t come across anything specifically saying that the string method doesn’t work with negative indices, including the link you referenced. But I did some more digging after your last reply, and found on someone’s blog that while the array prototype works this way, the string prototype doesn’t.
I had already passed the challenge using another method, and moved on, but I really wanted to understand why that solution wasn’t working, so thanks a ton for taking the time to reply and pushing me to dig deeper for the answer!
Here’s the blog post in case it helps anyone else: