The below solution doesn’t work for some reason. I know its a complex way to do it. I ultimately used str.split(’’).reverse(); to solve the problem but would like to understand why the below doesn’t work
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
if(str.length<target.length){
return false;
}
let strLength = str.length;
let targetLength = target.length;
console.log(str[strLength-1]);
console.log(target[targetLength-1]);
for(let i=0;i<targetLength;i++){
if(str[strLength-i] != target[targetLength-i]){
return false;
}
}
return true;
}
confirmEnding("Bastian", "n");
this is undefined at first iteration
you don’t have anything at str[strLength] (as i at first iteration is 0)
and if the target has length of 1, that is also the only thing you check, as after that the loop stops