Confirm the ending of a string Challenge:


  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. But for the
  purpose of this challenge, do not use it.


I would like to especially have my solution critiqued.

function confirmEnding(str, target){
  let lengthOfString = str.length;
  let lengthOfTarget = target.length;
  let subtractLengths = lengthOfString - lengthOfTarget;
  let lastIndexOfString = str.lastIndexOf(target);

  if(str.includes(target) === true){
    if(lastIndexOfString === subtractLengths){
      return true;
    }
    else{
      return false;
    }
  }
  else{
    return false;
  }
}


Test Cases:

confirmEnding("Bastian", "n") should return true.
Passed
confirmEnding("Congratulation", "on") should return true.
Passed
confirmEnding("Connor", "n") should return false.
Passed
confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.
Passed
confirmEnding("He has to give me a new name", "name") should return true.
Passed
confirmEnding("Open sesame", "same") should return true.
Passed
confirmEnding("Open sesame", "pen") should return false.
Passed
confirmEnding("Open sesame", "game") should return false.
Passed
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.
Passed
confirmEnding("Abstraction", "action") should return true.
Your solution is far too complicated.  It's also inefficient, because it uses functions .lastIndexOf() and .includes(), both of which analyze the entire str looking for target, whereas an optimal solution should look only starting at a known position at the end of str.

Here are two simple solutions:



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

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

console.log(
  false == confirmEnding("s", "try long target strings") &amp;&amp;
  false == confirmEnding("", "try an empty str") &amp;&amp;
  true  == confirmEnding("Bastian", "n") &amp;&amp;
  true  == confirmEnding("Congratulation", "on") &amp;&amp;
  false == confirmEnding("Connor", "n") &amp;&amp;
  false == confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") &amp;&amp;
  true  == confirmEnding("He has to give me a new name", "name") &amp;&amp;
  true  == confirmEnding("Open sesame", "same") &amp;&amp;
  false == confirmEnding("Open sesame", "pen") &amp;&amp;
  false == confirmEnding("Open sesame", "game") &amp;&amp;
  false == confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") &amp;&amp;
  true  == confirmEnding("Abstraction", "action")
)




The first solution, using .startsWith(), should be efficient, but it might be considered "cheating" to use .startsWith() even though only .endsWith() is prohibited.

The second solution is slightly simpler, but it involves creating a substring, so it would be less efficient.