Confirm the Ending bonfire help

I’ve been having trouble with this challenge for a while now. The one test case I have not been able to pass is confirmEnding("Open sesame", "same") should return true. For some reason, it returns false, and when I checked to see what returns instead of false, it returns “ame” and just skips over “same”. Any reason why?

Here is my code:

function confirmEnding(str, target) {
  var arr = str.split(" ");
  var compare = arr[arr.length - 1];
  for (var i = 0; i < compare.length; i++) {
    compare = compare.substr(i);
    if (compare === target) {
      return true;
    }
  }
  return false;

EDIT:
Here is what’s showing up in the console when I see what prints for each compare. Why does it skip “same”? Or is something else happening here?

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

1 Like

Thanks a bunch! I’ll be sure to read up on your post. Do you know what’s causing that one test case to fail in my code?

I’m testing it myself now…

I’m actually finding that more than one test is failing. The first major problem is that the for loop returns true under certain conditions which means in those instances return compare never gets reached.

@JacksonBates - Oops - used return compare for testing purposes. Just replaced it with return false.

1 Like

Still don’t know what’s wrong with the code though, it seems like it should work perfectly even for that test case. Does .substr skip a letter if it found it before?

One thing that might help you troubleshoot your code is to use console.log to test whether the code is doing what you think it should.

Here is a codepen example of your code with some descriptive console logging. (I used codepen rather than the FCC code window, because the FCC test runner would spew out LOADS of console logs…you just want to test the broken one :))

http://codepen.io/Malgalin/pen/ObRXZZ?editors=0012

Have a tinker with with to see if it helps clarify things…

@JacksonBates - thanks for that! I also looked at the console in Chrome’s web inpsector and put it as a screenshot above. It misses “same” for some reason. I’ll keep looking for the bug…

Yeah, so my method at this point would be to get a pen and paper and manually go through that loop myself - pretend you are the computer and do exactly what your program tells you to do. I find that often shows me why it returned what it did, rather than what I wanted it to return.

1 Like

My handwriting is not ideal, and the image may not be great quality…but look at that manual processing. If I were your computer, following your instructions, I would also skip same…

2 Likes

@JacksonBates - Thanks a bunch for doing this :smiley: really helpful, I’ll write things out in future debugging sessions. Guess it also helps to have more test cases so it’s not “just that one test case”. Solved it! (meant to post this yesterday but they were doing maintenance)