Confirm the Ending can't find the exit :(

Confirm the Ending can't find the exit :(
0.0 0

#1

Hi guys,
I know I asked about this challenge before and I’ve got some help (and thank you for that), but I’m stuck again and this time it’s almost at the end of it! I can feel the solution is somewhere near but I just can’t find it…

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
  var answer;
  for(var i=str.length-1, char = str[i];i>=0; i--){
    for(var j=target.length-1, tarChar = target[j]; j>=0; j--){
      if (char === target){
         answer = 1;
       }else answer = 0;
    }
    
  }
  if (answer === 1) {
    return true;
  }
  else return false;
 // return str;
}

confirmEnding("Bastian", "n");

Link to the challenge:


#2

Mh…i noticed a typo ( you used target after declaring tarChar ) but that’s not the point i think :confused:
It seems you check answer value, but it change for every character. That’s mean you’re checking if the LAST character is the same (even if you compare every char of the target string with every char of the base one).

I would suggest you to play with the substr methods and the string.length property: something like extract a number of characters from the base string ( from the end of it) equal to the target string length, and then try to compare them ^^

Hope it helps,
-LyR-


#3

Thank you so much!
(for the typo too :wink: )
thou i solved it a bit differently:


function confirmEnding(str, target) {
  var answer;
  for (var i=target.length-1, j=str.length-1; i>=0;i--){
    if (target[i]!==str[j]){
      answer = false;
    }else answer =true;
    j--;
  }
  
  return answer;
}

confirmEnding("Bastian", "n");