Why is my For loop not working?

Tell us what’s happening:

The for loop runs only once :open_mouth: ! can someone tell me what I am doing wrong ?

Your code so far


function confirmEnding(str, target) {
let flag = 0;
for( let x = target.length ; x > 0 ; x--){
  console.log(x);
  if(str[str.length-target.length+x-1] != target[x-1]){
  flag = 1
}
if ( flag == 0){
  return true;
}
else {return false;}
}
}

confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36.

Challenge: Confirm the Ending

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/confirm-the-ending

It reaches return (returns true) and that stops the execution of the function.

3 Likes

Thanks - shucks - I thought I put the if statement outside the loop ! :slight_smile: