Tell us what’s happening:
I am trying to see if the target parameter matches the end of the str parameter without using the endsWith() string method. I console.log() each line of code and everything seems to be called/accesses properly.
What would be wrong with my logic below? I am trying to compare the last X indexes in the str parameter. I would do that using .substring() and starting from ((str.length - 1) - (target.length - 1))
Your code so far
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
str.substring((str.length -1)-(target.length-1)) === target ? true : false;
}
confirmEnding("Bastian", "n");
Its is really good question that you brought up here. I checked your solution it works fine. You have to understand that substring is a function and when you return true or false. it is basically completing that function. Please remember you are not returning true/false for the confirmEnding function. You can easily do that by storing the return value of the substring function in a variable and return that variable.
I find that you ternary operator needs “return” and that you should use substring lengths of target and str. Your ternary could work without errors as below:
Keep in mind that you use -target.length instead of -1 since target is dynamic. substring() parameters can be in reverse order as long you pass one higher than the other, Javascript will understand that the lower value is the starting index and higher value the end.
Hope this helps