Help with Confirm the ending


I’m having troubles with this challenge.

From what I understood, the string variable should be matched to reflect the size of the target variable. I’m pasting my code and hope the get some help from you.

Using the following example:

confirmEnding("Open sesame", "same");

we are trying to determine if the letters “same” (the 2nd function argument) are the same as the last letters of “Open sesame” (the 1st function argument).

You can take the following steps to determine this:

Step 1) Find the number of letters (or length) of the 2nd argument. In our example, that would be 4 for the word “same”.

Step 2) Check to see if the last 4 letters of “Open sesame” are “same”. You can use the javascript function substr to capture the characters of a string starting at a specified index (first parameter) through a specified number of characters (second parameter). If you leave the second parameter off, it will return the characters from the specified index of first parameter all the way through the end of the string. Also, if we use a negative number for the first parameter, then it will start back from the end the specified number of characters and then work the same as with a positive number when using a second parameter or not. So, for our example, we want to capture the last 4 characters of “Open sesame” by using str.substr(-4) and assign it to another variable.

Step 3) Finally, we want to check if target is equal to the variable we created in Step 2. If they are equal, return true. Otherwise, return false.

See if you can figure out how to solve the problem now.


@rmdawson71 just gave you the answer. :slight_smile:
Hint: read how substr behaves with negative numbers as parameters.
confirmEnding() literally can be a one-line function.

Hi rmdawson71,

thank you for taking time for replying to my question.

When I posted on the forum it was bedtime for me and didn’t read your see your reply before morning (I believe we live in different time zones).

In any case, I actually managed to pass the challenge half an hour later by myself. However, I believe my implementation is very sub optimal and feels like I’m trying to cheat the FCC tests by making a tailored solution just to scrape the bottom of the barrell and go on instead of making a “universal” one. Furthermore, I noticed to use/think about a lot in if/else while many of these challenges could be solved way differently and better.

Here’s my code. I will take care to remove the link if it is needed.

Sorry for venting my thoughts to you but even though I’m passing these challenges so far, it feels like I’m not really advancing in learning how to solve problems in a more efficient way.

p.s.DId you actually post a one line solution and removed it later?


I did not post my one liner code, but I gave you all the steps to come up with a workable solution on your own.