Confirm the Ending struck

Confirm the Ending struck

Hello all, I’m trying to pass Confirm The Ending challange with my own algorithm and I’m doing something wrong in my code but couldn’t find it. Could you give me a handle please? Thank you :smiling_face_with_three_hearts:

function confirmEnding(str, target) {
let reverseStrArray = str.split("").reverse("");
let reverseTargetArray = target.split("").reverse("");

for (var i=0; i<reverseTargetArray.length;i++);{
    if (reverseStrArray[i]!=reverseTargetArray[i]){
        return false;
return true;

console.log(confirmEnding("Bastian", "an"));

For some reason you have a semi-colon at the end of your for loop declaration (before the {. Git rid of that, because it is causing a major problem.

Basically, with the semi-colon there the for loop consists only of the declaration and so it finishes and i = 2 (the last value of i after the i++. Then, the code block between the { and } is executed which compares the 3rd element of reverseStrArray (which is i) to the 3rd element of reverseTargetArray (which does not exist so it is the value undefined). Those two are not equal, so the function returns false.

1 Like