Todays challenge is easy: we must write a function that takes in two arguments. We must check if the first string ends with the given second string. Let me illustrate with an example:
end("Gorka Hernandez", "dez"); //-> Should return true, since the first string ends with "dez". (That's me by the way!) end("House", "cat"); //-> Should return false.
Let’s start by creating our function skeleton, since we know what it must do, even if we dont’t know how to do it yet.
function end(string, ending) { if (/* string ends in ending */) { return true; } else { return false; } }
Looks simple enough, now, how do we actually achieve this comparison? The answer is the String method substr(). Substr takes one argument (with an optional second argument) and returns a piece -or subselection- of the string on which it is called. The first argument will specify the start position (remember 0 based indexing!), and the second will tell the method how long the subselection should be. If no second argument is provided, substr will select everything from the start position to then end of the string. For example:
var str = "My name is Gorka."; console.log(str.substr(0, 2)); //-> Output: "My" console.log(str.substr(5, 4)); //-> Output: "me i" console.log(str.substr(12)); //-> Output: "orka."
But it get’s even better! If we use a negative number for the start position, substr will start counting from the end of the string, so:
var str = "My name is Gorka."; console.log(str.substr(-3)); //-> Output: "ka."
Try it out with different arguments, try using a length argument with a negative start argument and anything you can come up with! Doesn’t this use of substr actually seem useful for our current task? It would seem so!
We need to compare our end string, to the last x characters of the original string. So, if our end string is 4 characters long, we need to compare it to the last 4 characters of the original string. Who would’ve guessed, we just learned how to do this. If we take our previous version of the end() function and implement this logic, this is what we end up with:
function end(string, ending) { if (string.substr(-ending.length) == ending) { return true; } else { return false; } }
There we go! Working fine and fast! How about we make it shorter? Let me present to you, the ternary operator! The ternary operator is but a shorter way of implementing if/else comparisons. The syntax is as follows:
If/else:
if (age > 18) { return true; } else { return false; }
Ternary operator:
return age > 18 ? true : false;
Pretty cool eh? The ternary operator is composed by the ? and : characters, if the comparison before the ? symbol is true, we return true, else, we return false. The ternary operator can be nested for great complexity, but we we’ll not be doing that today. Instead, let’s create a version of the end() function by using the ternary operator:
function end(string, ending) { return string.substr(-ending.length) == ending ? true : false; }
And that’s that. Try it out, it may look confusing, but is can compress code pretty efficiently.
Keep your brain in check and code away!