My code is not passing two test cases


Basic Algorithm Scripting: Confirm the Ending 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, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.

confirmEnding(“Bastian”, “n”) should return true.
confirmEnding(“Congratulation”, “on”) should return true.
confirmEnding(“Connor”, “n”) should return false.
confirmEnding(“Walking on water and developing software from a specification are easy if both are frozen”, “specification”) should return false.
confirmEnding(“He has to give me a new name”, “name”) should return true.
confirmEnding(“Open sesame”, “same”) should return true.
confirmEnding(“Open sesame”, “pen”) should return false.
confirmEnding(“Open sesame”, “game”) should return false.
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.
confirmEnding(“Abstraction”, “action”) should return true.

i am unable to pass 4th and 8th cases with my current code

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
  var first = str.length;
  let last= target.length;
  for (var i = first-1; i>last-1; i--){
      return true;

    return false;


confirmEnding("Bastian", "n");

you are checking only one character, as you have both return statements inside the loop, and one of the two will be executed. once it is executed, the function stops generating an output.

an other issue you have is this - if you want to check each character of the target string against the same number of characters at the end of the str string, well, you are checking a variable character from str against always the same character of target, so it can’t work if target is longer than 1