# freeCodeCamp Algorithm Challenge Guide: Confirm the Ending

freeCodeCamp Algorithm Challenge Guide: Confirm the Ending
0

#1

### 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.

## 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

## 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

## Hint: 3

Check that you have the proper syntax and that you use `===` to compare.

try to solve the problem now

## Basic Code Solution:

``````function confirmEnding(str, target) {
return str.substr(-target.length) === target;
}
``````

### Code Explanation:

The `substr()` method returns the characters in a string beginning at the specified location through the optional specified number of characters. `substr` operates through the end of the string if the second optional parameter is not specified. `substr()` calculates the index of first matching character from the string’s end if the specified location is negative. Using the `-` operator in front of `target.length` makes it negative.

We use the method `substr()` with the negative value of `target`'s length to extract the ending segment of `str` of the same size as `target`, compare it to `target`, and then return the value of this boolean expression.

## NOTES FOR CONTRIBUTIONS:

• 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.
• Categorize the solution in one of the following categories — Basic, Intermediate and Advanced.

See `Wiki Challenge Solution Template` for reference.

#2

#3

#4

Great explanation! Thanks for breaking it down into such simple terms.

#5

I used this method to do this exercise, longer but effective.

function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor
var targetLength = target.length;
var sub = str.slice(-targetLength);
if(sub === target){
return true;
}
return false;
}

confirmEnding(“open sesame”, “same”);

#6

I used this

``````function confirmEnding(str, target) {

if (str.substr(str.length -target.length,str.length) !== target){
return false;
} else{
return true;
}

return str;
}

confirmEnding("He has to give me a new name", "name");
`````````

#7

function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor
return (str.lastIndexOf(target) === str.length - target.length) ? true : false;
}

confirmEnding(“Bastian”, “n”);

#8

Here is the way I solved this; this one took some thinking!

function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor
var anyString = str.length;
var x = target.length;
var anyStringLast = str.substring(anyString - x);
return anyStringLast === target;
}

#9

Why so complicated? Any compare operator returns true or false, so we don’t need no additional true-false.
Just:
return str.substring(str.length - target.length) === target;

It is possible to write more short function
#10

Here is my solution:

function confirmEnding(str, target) {

return str.substr(str.length - target.length) === target;
}
//test
confirmEnding(“Bastian”, “n”);

#11

I solved my own using this code snippet:

function confirmEnding(str, target) {
for(var i = 0; i < str.length; i++){
if(str.substr(i-str.length) === target){
return true;
}
}
return false;
}
confirmEnding(“Bastian”, “n”);

#12

Hi - don’t think I saw this approach being used yet, also a bit longer - adds an identifier onto the end of the string, and using the target to split the string it tests whether the identifier at the end of the string was successfully split off:

function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor

var str2 = str.concat(“test”);
var split = str2.split(target);

if (split[split.length-1]===“test”) {return true;}
else {return false;}

}

confirmEnding(“Bastian”, “n”);

#13

var v = str.substr(str.length-target.length );
if (target===v){
return true;
}
else {
return false;
}
return str;
}

#14

Now, I always thinking that operator returns true or false. For the challenge, i’ll reduce my code at a “if” with return true or flase. I learn every days.

#15

I used this approach to solve the problem:

function confirmEnding(str, target) {
var arr = [];
var string = str.split("");
for(var i = 1; i <= target.length; i++){
arr.push(string.length - i);
}
arr.sort(function(a, b){return a - b;});
var arr1 = [];
for(var j = 0; j < target.length; j++){
arr1.push(string[arr[j]]);
}
if(arr1.join("") === target){
return true;
}
return false;
}

I first converted “str” into an array of letters then i used a for loop to push to the array “arr” letters from the end of the “string” array equal to the length of the “target”, then i sort the array “arr” in an ascending order then i create another array “arr1” and used another for loop to push to the array “arr1” the actual letters that “arr” array refer to it using numbers after that i compared ‘arr1.join("")’ string to the “target” string if equal in value and type then return true, if not return false.

#16

Nice, that went really smooth for a change. Almost exactly like the provided solution, just with unnecessary length-checking.

return str.substr((str.length - target.length)) === target;

I took a look at the endsWith() function and it seems to work as follows:

``````    if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}``````

#17

function confirmEnding(str, target) {
var tl = target.length;
var sl = str.lenght;

var controle = str.substring(str.length-tl,str.length);
return controle==target;
}

confirmEnding(“Bastian”, “n”);

#18

function confirmEnding(str, target) {
var targetLength = target.length,
finalStr = str.substr(-targetLength),
trueOrFalse;

if(finalStr === target){
trueOrFalse = true;
} else {
trueOrFalse = false;
}

return trueOrFalse;
}

confirmEnding(“Bastian”, “n”);

#19

Basic solution
This is done without substr, simple procedural reasoning,
linear type.

``````function confirmEnding(str, target) {
for (var i = 0; i < target.length; i++) {
if (target[target.length - 1 - i] != str[str.length - 1 - i]) {
return false;
}
}
return true;
}``````

#20

I must have the most complicated and embarrassing approach of all:

``` function confirmEnding(str, target) { var array = str.split(" "); if (array.length == 1 && array[0].substring(array[0].length - 1) == target) { return true; } var lastWord = array[array.length - 1]; if (lastWord.substring(lastWord.length - target.length) == target){ return true; } return false; ``` I got arrays on the brain.