Build a Confirm the Ending Tool - Build a Confirm the Ending Tool

Tell us what’s happening:

Hi there,

I’m so confused about how I’m getting the results that I am on this one.

I’m hoping someone here can help me understand how the method I’m using is failing two of the checks (5 & 6), yet passing all of the others.

Thanks in advance!!

Your code so far


const confirmEnding = (str1, str2) => {
  let charsFromEnd = ~str2.length + 1;
  return (str1.includes(str2, charsFromEnd)) 
};  


let result = confirmEnding(str1, str2);
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36

Challenge Information:

Build a Confirm the Ending Tool - Build a Confirm the Ending Tool

with this line your code is not going to pass any test, you get an error ReferenceError: str1 is not defined

I would check if charsFromEnd is really having the value you expect here

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?

All of the other checks are passing.

are you sure that includes work with negative indeces?

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: