
Confirm the Ending
Today’s installment of Basic Algorithm Scripting, we will be given two strings. One string is a word or a sentence, and the second string (the target string) is a either a word or a letter. The task is to determine if the first string ends with the target string. If it does, the function will return true. If not, return false. The first string could be either a word like for example:

The first string can also be a sentence:

You’ll need to check whether or not if the first string is just a word or a sentence. It helps to split the string into an array. If the array is more than one word or item then it must be a word. If not, then it must be a sentence.


To check if the word’s final letter matches the target letter. Since computers count the letters within strings starting from zero, if you wanted to find the last letter, the index number will be the length of the string minus one. To get the ‘k’ in ‘bork’, assuming the word ‘bork’ is in a variable, it’ll be: variableName[variableName.length-1].


Now for the above example, if the target letter is f, the function will return false. But if target was ‘k’, it’ll return true. The same thing applies for sentences. The difference is after splitting the sentence, you’ll focus on the array itself instead of just a string. And in that array you’ll be looking at the last word or the last item in the array and compare it to the target string:


If it matches, return True but if not, return False. But look at the example above. You probably think the target word can either be an entire word or just a letter. For the example above, if the target was ‘book’, the function will return true but what about if the target was ‘ook’. You’ll probably think it should return:

When really, it should return:

How? You may ask. Well ‘book’ does end with ‘ook’. It’s not about whole words or the final letter. For the word ‘book’, ‘ook’, ‘ok’, and ‘k’ will return true. As long as the those letter combinations as a whole form the ending of the word, they will return True. ‘Bo’ and ‘oo’ will not work because, even though they are part of the last word of “Bork reads a book” those letter combinations don’t end the word ‘book’, they are instead in the middle or in the beginning of the word. How does one check to see if letter combinations are located at the end of the word. Let’s bring in Mr. IndexOf (Javascript’s favorite method…kind of):

Mr. IndexOf will determine if ‘ook’ is in ‘book’ and return a value where ‘ook’ is first found. If you look at the lovely drawing, ‘ook’ is first found on the second letter of ‘book’. If it couldn’t find anything then it will return a -1. Next up is using another javascript method: substring

With substring you look for a string segment starting with the number Mr. IndexOf output all the way to the end of the string. If that segment matches the target string, then it will return true.
So in addition to checking if a target string matches the last letter of a word or the last word of the sentence. Also check if the target string matches the last segments or last few letters of a word.