freeCodeCamp Challenge Guide: Confirm the Ending

I had answer very similar to basic code, using a ternary operator, in hindsight I could’ve condensed it down to the basic solution

function confirmEnding(str, target) {
  return (str.substr(-target.length) == target)  ? true  : false;
}

confirmEnding("Bastian", "n");
3 Likes

my code:
function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor
for (var x=0 ;x < str.length; x++) {
if (str.substring(str.length-x) === target.substring(target.length-4,target.length))
return true;
}
return false;
}

confirmEnding(“Bastian”, “n”);

Hello, i used slice for solve the challenge.
Here is my code
function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor
return str.slice(-target.length) == target;
}
confirmEnding(“Bastian”, “n”);

As always here is my new way to do it

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
  return str.substr(-target.length) === target;
}
3 Likes

My solution was pretty much the same as the basic code answer. However I did try to solve it with the .endWith() method as well. I’ll include that below for comparison.

 function confirmEnding(startOfString, endOFString) {
        return startOfString.substr(-endOfString.length) === endOfString;
}
confirmEnding("Bastian", "n");

Personally I like this solution better as it seems much simpler to understand and requires a little bit less code

function ending(startOfString, endOfString) {
  return startOfString.endsWith(endOfString);
};

ending("Pickle", "t");

Alright, here’s my code for this exercise:
function confirmEnding(str, target) {

var array= str.split(" ");

if (str.substr(str.length-target.length) == target){
return true;
} else if (array.length>1 && array[array.length-1] == target ){

return true;

}
return false;
}

confirmEnding(“Bastian”, “n”);

I got a similar code but used .substring instead of .substr

function confirmEnding(str, target) {
if (str.substring(str.length - target.length) === target) {
return true;
}
return false;
}

1 Like

My solution:

function confirmEnding(str, target) {

if(str.substring(str.length - target.length) === target) {
return true;
} else {
return false;
}
}
confirmEnding(“Bastian”, “n”);

1 Like

Hi everyone, I solved this using the string method .includes() after I split the sentence/word into an array of words/letters. Do you think this is cheating or is this fine?

function confirmEnding(str, target) {
  var splitStr;
  if (str.includes(" "))
    splitStr = str.split(" ");
  else
    splitStr = str.split("");
  
  var lastPart = splitStr[splitStr.length - 1];
  
  return lastPart.includes(target);
}

confirmEnding("Bastian", "n");
1 Like

Hey campers,this was my approach:

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

var lastIndexOf = target;

if(str.lastIndexOf(target) == (str.length - target.length));

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

}

confirmEnding(“Bastian”, “n”);

Hi all,This is how I did it.
function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor
var ne = str.substring(str.length -target.length);
if (ne === target){
return true;
}
return false;
}

confirmEnding(“Bastian”, “n”);

// This is what I got :

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

Hi guys this is my take on the problem:

function confirmEnding(str, target) {
// “Never give up and good luck will find you.”
// – Falcor
var car = str.split(’’);
var cCar = target.split(’’);
if(target == str.substr( car.length - cCar.length))
return true;
else
return false;
}

confirmEnding(“He has to give me a new name”, “name”);
Would like to know what’s there to improve on this…

I think this is Basic Code Solution
Checking if here is one word or more and our solution goes to the right branch

function confirmEnding(str, target) {
 
  var arr = str.split(' ');
  if (arr.length > 1) { 
  return arr[arr.length - 1].substring(arr[arr.length -1].length - 1 - (target.length - 1)) == target ;                  
  } else { 
   return str[str.length - 1] == target ;
  }
}
1 Like

A little similar

function confirmEnding(str, target) {

return str.split(' ').pop().substr(str.split(' ').pop().length-(target.length),target.length)===target;

}

confirmEnding(“Bastian”, “n”);

1 Like

This is how I was able to solve the problem. Hey it worked :grinning:

edit* Made it easier to read the code

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
  str = str.split(' ');
  for (x = 0; x < str.length; x++) {
    lastWord = str[x];
  }
  lastWord = lastWord.split('').reverse();
  target = target.split('').reverse();
  
  var j = true;
  for (i = 0; i < target.length; i++) {
 
    if (target[i] !== lastWord[i]) {
      j = false;
    } else j = true;
  } return j;
}
1 Like
function confirmEnding(str, target) {
  return str.substring(str.length - target.length) === target;
}

confirmEnding("Bastian", "n");
2 Likes
function confirmEnding(str, target) {
  return (target == str.substr(str.length - target.length, target.length));
}

confirmEnding("Bastian", "n");

function confirmEnding(str, target) {

var splited = str.split(" ");
if (target === splited[splited.length-1]) {
return true;
}
else if (target === str[str.length-1]) {
return true;
}
else if (target === splited[splited.length-1].substring((splited[splited.length-1].length - target.length))){
return true;
}
else {
return false;
}
}

After hour… Guess i am bad too . Nevermind

my solution

function confirmEnding(str, target) {
  var amount = target.length;
  var finalChar = str.substr(-amount, amount);
  if (finalChar === target){
    return true;
  }
  return false;
}
1 Like

After seeing the other answers, I am a little embarrassed. But this is what I got:

function confirmEnding(str, target) {

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

confirmEnding("Bastian", "n");
6 Likes