Two ways to confirm the ending of a String in JavaScript
In this article, I’ll explain how to solve freeCodeCamp’s “Confirm the Ending” challenge. This involves checking whether a string ends with specific sequence of characters.

There are the two approaches I’ll cover:

using the substr() method using endsWith() method

The Algorithm Challenge Description

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.

Provided test cases

confirmEnding("Bastian", "n") should return true. confirmEnding("Connor", "n") should return false. confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false. largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

confirmEnding("He has to give me a new name", "name") should return true. confirmEnding("Open sesame", "same") should return true. confirmEnding("Open sesame", "pen") should return false. confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false. Do not use the built-in method .endsWith() to solve the challenge.

Approach #1: Confirm the Ending of a String With Built-In Functions — with substr()

For this solution, you’ll use the String.prototype.substr() method:

The substr() method returns the characters in a string beginning at the specified location through the specified number of characters.

Why are you using string.substr(-target.length) ?

If the target.length is negative, the substr() method will start the counting from the end of the string, which is what you want in this code challenge.

You don’t want to use string.substr(-1) to get the last element of the string, because if the target is longer than one letter:

confirmEnding("Open sesame", "same")

…the target won’t return at all.

So here string.substr(-target.length) will get the last index of the string ‘Bastian’ which is ‘n’.

Then you check whether string.substr(-target.length) equals the target (true or false).

Without comments:

You can use a ternary operator as a shortcut for the if statement:

(string.substr(-target.length) === target) ? true : false;

This can be read as:

if (string.substr(-target.length) === target) {

return true;

} else {

return false;

}

You then return the ternary operator in your function:

You can also refactor your code to make it more succinct by just returning the condition:
['Sonya Moisset']
['Technology', 'JavaScript', 'Learning', 'Algorithms', 'Programming']