Hello everyone, I was wondering if someone could help me figure out this Codewars issue I am having. I have attached an image with the problem at hand. I have tested the function on a separate IDE and the function does find the last letter and test to see if it true or false. However when I try to use my function on the codewars it flags the error “expected false to equal true”. What does this error mean in this case?
I’m not sure your code is doing what the challenge is asking for. Try adding a console.log for lastItem before the if statement. If the second arg is more than one character then I think you have a problem.
I tried the challenge now and this is my code, I have kept comments to explain my thought process. If you don’t understand, I am happy to provide more clarification.
function confirmEnding(str, target) {
// length of the string
const lenStr = str.length;
// length of the target
const lenTarget = target.length;
// if you pass a single arguement to the string.substring method,
// it'll return the substring starting from the position you passed
// so here, I tell it to return the substring starting from the string's
// length - the target's length.
const strSubString = str.substring(lenStr - lenTarget)
// check if the returned substring is equal to the target
return strSubString === target;
}
I added spoiler tags around your code so it is not to spoiler the answer for those who have not worked on this FCC challenge yet.
But also, it will be a better educational experience if you help guide the user through hints instead of providing them with the full answer with comments in the code.