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

Tell us what’s happening:

Test Number 6 does not work. I know what the problem is but this is maximum capacity my brain works and how I can do this.
So , I need some help;;;

Your code so far

function confirmEnding(sentence, check){
  let toCheck = sentence.includes(check);
  const endSentence = sentence[sentence.length - 1];
  const endCheck = check[check.length - 1];
  if (toCheck == true && endSentence == endCheck){
    console.log(true);
    return true
  } else {
    console.log(false);
    return false
  }
}

confirmEnding("Congratulation", "on");
confirmEnding("Bastian", "n")
confirmEnding("Connor", "n")
confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification")
confirmEnding("He has to give me a new name", "name")
confirmEnding("Open sesame", "same")
confirmEnding("Open sesame", "sage")
confirmEnding("Open sesame", "game");
confirmEnding("If you want to save our world, you must hurry. We don't know how much longer we can withstand the nothing", "mountain");
confirmEnding("Abstraction", "action")



Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.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

Your code is only looking at the last letter, so even though it passes some of the tests, the logic is incorrect. Debug by logging your variables so you can see what you’re working with.

I know that I just don’t know how else I am supposed to do it. Should I use code which looks at the last 2 letters? I found a way which involves .substr() but I don’t wanna use that because it was not in the theory.

Your task is to determine if the substring check is at the end of the string sentence, right? So, no, looking at only the last two letters will not cut it. Think about how you can grab the same numbers of letters in check from the end of sentence and compare.

I did it like this

function confirmEnding(sentence, check){
const endSentence = sentence.slice(sentence.length-check.length);
if (check === endSentence){
console.log(true);
return true
} else {
console.log(false);
return false
}
}

1 Like

I did similar to yours. One difference is the slice method. I wrote:

sentence.slice(-check.length)

which abstract the string from the last of sentence.