Confirm the Ending

I’ve looked at the answers and see that there are easier ways to complete this challenge but i was wondering if anyone could see what was going wrong with my code? It matches for one word inputs but not for multiple inputs.


function confirmEnding(str, target) {
  let word = ""
  for (let i = 0; i < target.length; i++) {
  
    if (target[i] == str[str.length-i-1]) {
      word += target[i];
    
    } 
  }

if (word == target) {
  return true;
} else {
  return false;
}
  
  
}

confirmEnding("Congratulation", "on");

Can you please provide a link to the lesson?

Please explain what you think this logic is doing for your solution.

Add the following console.log statement on a new line before the above if statement, so you can see what you are actually comparing and why this comparison is not doing anything for you. It is strictly by chance alone that you got the first test to pass. The only reason some of the other tests passed is that they were supposed to return false and since your logic was not going to make anything where the target was greater than one character in length return true, your function returns the correct value of false by coincidence.

i figured for the case congratulation, on when:
i = 0
target[0] = o
but string[length-0-1] = N

but i just wrote it out and see that it is going in the opposite order

Good job debugging on your own!

took a while but i figured it out now i have a headache. its still hard for me to remember all the commands to make things easier

function confirmEnding(str, target) {
  let word = ""
  for (let i = 0; i < target.length; i++) {
    
let x = ((target.length) - i)

console.log([x])
console.log(str[str.length-x])
    if (target[i] == str[str.length-x]) {
      word += target[i];
    
    } 
  }

if (word == target) {
  return true;
} else {
  return false;
}
  
  
}

confirmEnding("Congratulation", "on");