freeCodeCamp Challenge Guide: Confirm the Ending

freeCodeCamp Challenge Guide: Confirm the Ending
0

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.

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;
      };
    }
1 Like

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”);

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”);

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;
}
1 Like

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.
1 Like

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");

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;
}

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;
}

My solution:

function confirmEnding(str, target) {

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

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 ;
  }
}

A little similar

function confirmEnding(str, target) {

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

}

confirmEnding(“Bastian”, “n”);