Confirm the Ending
Learn how to solve the freeCodeCamp algorithm 'Confirm the Ending' using the String.slice() method.
In this freeCodeCamp algorithm we need to check if a given string ends with a specified 'target' string.
Spec
Check if a string (first argument, str) ends with the given target string (second argument, target).
This challenge can be solved with the .endsWith() method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.
Requirements
confirmEnding("Bastian", "n")
should returntrue
confirmEnding("Congratulation", "on")
should returntrue
confirmEnding("Connor", "n")
should returnfalse
confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification")
should returnfalse
confirmEnding("He has to give me a new name", "name")
should return trueconfirmEnding("Open sesame", "same")
should returntrue
confirmEnding("Open sesame", "pen")
should returnfalse
confirmEnding("Open sesame", "game")
should returnfalse
confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain")
should returnfalse
confirmEnding("Abstraction", "action")
should returntrue
- Do not use the built-in method
.endsWith()
to solve the challenge.
Psuedocode
// Create a function that accepts 2 strings, str and target
// Slice the end of str so it's the same length as target
// Compare the newly-sliced str to target
// Return true or false
Solving the Algorithm
Setting up our function
Let's setup our function to accept two arguments, str
and target
.
function confirmEnding(str, target) {
}
Slice the end of str
so it's the same length as target
So if we think about this logically, we want to take str
and make sure it ends in whatever target
is. It shouldn't matter what target
is or it's length, we just need to check to make sure it matches the ending of str
.
We can accomplish this by using the String.slice()
method where we just pass in the beginIndex
as per the documentation. The beginIndex
to start the slice should be the length of the str
minus the length of target
which leaves us with just the ending piece of str
that is of equal length to target
.
Let's try it out:
function confirmEnding(str, target) {
str = str.slice(str.length - target.length);
console.log('str:', str);
console.log('target:', target);
}
confirmEnding('Open sesame', 'same');
Return the comparison of str
and target
Okay, it looks like we've pretty much got the answer we want from the last step. We just need to return the result of a comparison between str
and target
. Easy enough, right?
function confirmEnding(str, target) {
return str.slice(str.length - target.length) === target;
}
confirmEnding('Open sesame', 'same');
Final Thoughts
This freeCodeCamp algorithm was pretty short and sweet for the most part.
Hopefully you found this to be a helpful walkthrough on how to confirm the ending of a string!
Shoot me an email at [email protected]
with any questions and if you enjoyed this, stay tuned and subscribe below! 👇
Help us improve our content