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

Tell us what’s happening:

Hi, I’m struggling to find a way to explicitly determine what the end of str1 is, which is leading my output to return true on “connor”, simply because it contains the letter “n”. I know it should return false as the last letters aren’t identical.

I’ve tried lastIndexOf(), slice(), indexOf() and substring(), all of which create some of the solution but don’t compare the exact string.

I’d like to somehow compare the last letters of the string dynamically.

Your code so far

const sentenceEnding = (str1, str2) => {
  if (str1.includes(str2.slice(-1))) {
    return true
  } else
    return false
};

console.log(sentenceEnding("Connor", "n"))

Your browser information:

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

Challenge Information:

Build a Confirm the Ending Tool - Build a Confirm the Ending Tool
https://www.freecodecamp.org/learn/full-stack-developer/lab-string-ending-checker/implement-a-string-ending-checker-function

let’s take a look at your code, do you understand why it does not work?

what does includes do?

Thanks for your reply,

It determines whether a given string is included within another string. In this case whether str1 is found in str2

It doesn’t work because there’s nothing in it to check whether str1 and str2 are identically formatted rather than just whether they include the same letters.

so do you think that is an appropriate tool to check the end of a string?


consider, if you had pen and paper and had to explain to a 3 year old child how to check if a word ends in a determined way, how would you explain it?

I feel like I could hard code something to check whether str1.slice(-4) is the same but that wouldn’t make it dynamic anymore

Hmm, I feel I would reference the given word e.g. “Hello” end with “llo”. Where I’m struggling is establishing the rule.

do you think if you say that sentence to a 3 year old they would be able to check if a word end in a certain way?

try to explain the steps to a child

Okay then… To check whether the phrase you have written down, matches the end of my phrase I gave you. You need to look carefully at the letters of the end of the phrase, to see if they match yours.

would you be able to give it even more details?

I would say, you can count back from the end, checking each letter as you go and if they are all correct, then it’s a match.

However this sounds like a loop which I haven’t covered properly yet.

how many letters do you need to check at the end?

This is where I’m getting stuck. I want to check for as many as needed, as long as they are correct.

in the email checker lab using indexOf(“@”) worked, but this won’t apply as the string could be anything and any length. So how can you determine whether something is both there and in the right order?

can you know in advance how many you need to check?

like, how many letters for sentenceEnding("Connor", "n")?
and how many for confirmEnding("He has to give me a new name", "name")?

Yes, with the .length property

It’s piecing it together that I’m finding tricky. So I now know how to find the string length, but if I then check if they’re both equal, it will say no if the string length isn’t the same

do you know how to get a piece of a string?

Yes, using slice. Thanks, I now know what to do. Select str1, but only the amount of str2.length (from the end of the string). Then compare that with str2.

I just need to figure out how to do it as the behaiviour with .length and .slice seems a little quirky. Thank you for you help