I just worked through the excercise “Confirm the Ending of the String” and my final solution was basically to compare a substring to the target:
` var string = str.substring(str.length -target.length);
if(string == target) {``
Like that. But I’m wondering my other two, original ideas didn’t work, specificially they worked on every test except:
confirmEnding(“Walking on water and developing software from a specification are easy if both are frozen”, “specification”) should return false.
` var check = str.split("");
var length = check.length-1;
var targetSplit = target.split("");
var targetLength = targetSplit.length-1;
var checkLength = length-targetLength;
if(check[length] == targetSplit[targetLength]) {
return true;
} else {
return false;
}
When that didn’t work I thought perhaps I needed to reverse and have a loop iterate through each letter at the end of the string going backwards:
var reverseStr = str.split("").reverse();
var reverseTarget = target.split("").reverse();
var yes = true;
for(var i = 0; i < reverseTarget.length; i++) {
if(reverseStr[i] == reverseTarget[i]) {
console.log(reverseStr + " and " + reverseTarget);
return true;
} else
return false;
}
}
I figured it out so it’s sort of moot but I want to know why neither of the above solutions work, specifically with that one question.