Confirming the End - Algorithm Challenge

Confirming the End - Algorithm Challenge
0

#1

Hi fellow campers,

My question is regarding the solution that I’ve written for the the Algorithm challenge located here:

https://www.freecodecamp.org/challenges/confirm-the-ending

All of the test strings checked out beside this one.

Does anyone know why and have a suggestion to how I should modify my solution(without changing entire statement)?

function confirmEnding(str,target) {
   var firstString = str.substring(str.length - 1);
   var secondString = target.substring(target.length -1);

if (firstString === secondString) {
  return true; 
  } else {
  return false;
  }
}

PS: Hopefully my question is understandable, this is my first time posting in the help section.

Thank you all. :grinning:


#2

Your solution is only comparing the last character of each str and the last character of target.

Why/How? When you write the following:

firstString = str.substring(str.length - 1);

str.length -1 is the number 88. When you use the substring function with only one parameter, it returns a string starting at the index specified by the parameter value and goes through the end of the string. In this case, it returns just the “n”.

The variable secondString also gets the last character of the string, based on what I described above.

The reason you passed the other tests is just coincidence.


#3

Thanks for the breakdown Randell. I am slowly breaking away from the coincidence approach. :wink: