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?

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");

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.