My code is not passing two test cases

1

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.
Passed
confirmEnding(“Congratulation”, “on”) should return true.
Passed
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.
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.
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.

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--){
    if(str[i]===target[last-1]){
      return true;

    }
     else{
    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