[SOLVED] Confirm the ending working except one line

[SOLVED] Confirm the ending working except one line
0

#1

I’m not quite sure why my program isn’t working.

Here is the code:

function confirmEnding(str, target) {

  for(var x = str.length-1; x < str.length; x++){
      for(var j = target.length-1; j>=0; j--){
          if(str[x] === target[j]){
              return true;
          } else{
             return false; 
          }
      }
  }
  
}

confirmEnding("Connor", "n");

Everything works except that 1 line of
"confirmEnding(“Walking on water and developing software from a specification are easy if both are frozen”, “specification”)"

That one doesn’t return false, so I can’t pass the challenge.

I do not want to write the program the regular way of

return str.substring(-target.length) === true;

If anyone can provide a solution to the code i provided, it’ll be much appreciated !


#2

There’s an issue with the first for-loop header. You’ve set x to be str.length - 1, the condition to be x < str.length, and increment x. With that header, you’re only checking the last letter of str. You want to set x such that the for-loop checks as many letters in str as there are in target.

Also, you don’t want to put an if-else block in the for-loop where both of the branches return a value. That will cause the for-loop to stop running right after the first loop.


#3

Thank you,

I’ve solved it.

function confirmEnding(str, target) {

  for(var x = 0; x < target.length; x++){
      if(str[x + str.length - target.length] !== target[x]){
          return false;
      }
  }
  return true;
  
}

confirmEnding("Connor", "n");