Please help with Codewars error!

(Training on String ends with? | Codewars)

Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).


solution('abc', 'bc') // returns true
solution('abc', 'd') // returns false
function solution(str, ending) {
  const splitStr = str.split("");
  const lastItem = splitStr[splitStr.length - 1];
  if (lastItem == ending) {
    return true
  } else {
    return false
Sample test:
describe("Tests", () => {
  it("test", () => {
Test.assertEquals(solution('abcde', 'cde'), true)
Test.assertEquals(solution('abcde', 'abc'), false)

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?

Please post code and links instead of pictures. It is very hard to debug pictures.

Fixed it, sorry for that.

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.

1 Like

Hi @davidflores11724 !

This codewars challenge is the exact same one as this FCC challenge.

I don’t think there is a need to use the split method.

I would hop on MDN docs and poke around and see if you can find a handy method to do what the challenge is asking from you.

1 Like

Hi @davidflores11724

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;
1 Like

Hi @codekagei !

Welcome to the forum!

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.


Hi @jwilkins.oboe thank you!

My apologies, I just joined and I didn’t take note of that. Thank you again and I’d take note of your suggestion.

1 Like