Tell us what’s happening:
i am trying to loop the items in an array starting from the last item in that particular array. but .length-1 gives me the index and not the item at that index
target is a string (of one character) and newArr.length-1 is a number so you can’t compare these two to see if they are equal because they will never be. In the if condition you want to compare the value of the last entry in newArr with the target. There are many ways to get the last value in an array. How would you get the first value in an array?
Not saying this is the wrong approach, but it may be that you’re over-thinking this a little. Have you taken a look at String.substring(…) yet? It may be a more straightforward route to what you’re trying to do.
Consider the logic - you want to compare the last <length of comparator> letters of <string> to <comparator> itself. .substring(...) can help you do just that.
See, you got to remember, you might not only have confirmEnding("Bastien", "n"), you might also get confirmEnding("Congrats", "rats") - and your current approach won’t really allow that.
I’m really curious to see how you’ll get that working via an array like that. I mean, maybe you could convert the comparator to an array as well, and then compare using negative indices on both…
Actually, to do this? You don’t even need to convert them to an array. Strings are already “array-like” in javascript, in that you can do something like str[str.length-i], and it will access the string position that way.
yea, the only reason i coverted to arrray is because i want a situation where by i can be able to access the last element in the array and other item before it