Algorithm Confirm The Ending - thelastmile/FreeCodeCamp GitHub Wiki
Algorithm Confirm the Ending
:checkered_flag: Problem Explanation:
The function is a whole Boolean operation. You need to return true if the first argument ends with the second argument. This means that for the problem script, it should return true for the confirmEnding('Bastian', 'n');
case.
Relevant Links
:speech_balloon: Hint: 1
Take a look at how substr() works. You will be trying to get the last Nth characters.
try to solve the problem now
:speech_balloon: Hint: 2
To get the Nth-to-Last character you will use length() and turn it into a negative number.
try to solve the problem now
:speech_balloon: Hint: 3
Check that you have the proper syntax and that you use ===
to compare.
try to solve the problem now
Spoiler Alert!
Solution ahead!
:beginner: Basic Code Solution:
function confirmEnding(str, target) {
return str.substr(-target.length) === target;
}
:rocket: Run Code
Code Explanation:
We use the subtring() with the negative value of the lengths of target. We could use -1 to get the last element but if the target is actually longer than one letter then the program will provide the wrong information. Then we return the value of this boolean expression.
:trophy: Credits:
If you found this page useful, you can give thanks by copying and pasting this on the main chat:
Thanks @Rafase282 for your help with Algorithm: Confirm the Ending
:clipboard: NOTES FOR CONTRIBUTIONS:
- :warning: DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
- Add an explanation of your solution.
- Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. :traffic_light:
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See :point_right:
Wiki Challenge Solution Template
for reference.