Post can also be found on my website, https://virenb.cc/fcc-006-confirm-the-ending
Let's solve freeCodeCamp's Basic Algorithm Scripting Challenge, "Confirm the Ending"
Our Starter Code (& Tests)
function confirmEnding(str, target) {
return str;
}
confirmEnding("Bastian", "n");
// Tests
confirmEnding("Bastian", "n") should return true.
confirmEnding("Congratulation", "on") should return true.
confirmEnding("Connor", "n") should return false.
confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.
confirmEnding("He has to give me a new name", "name") should return true.
confirmEnding("Open sesame", "same") should return true.
confirmEnding("Open sesame", "pen") should return false.
confirmEnding("Open sesame", "game") should return false.
confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.
confirmEnding("Abstraction", "action") should return true.
Your code should not use the built-in method .endsWith() to solve the challenge.
Our Instructions
Check if a string (first argument, str
) ends with the given target string (second argument, target
).
This challenge can be solved with the .endsWith()
method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.
Thoughts
- We have two inputs, both being string data types
- Looking at the tests, our output must be a boolean value (true or false)
- Do not use
.endsWith()
method in our solution - What we need to do is compare two strings
Further Thoughts
Our first input, str
, is always longer than the second input, target
. str
is normally a word or a sentence.
Strings have a few properties and methods. String.length
tells us how long our string is.
A method that would help us solve this is String.substring()
. It will return a part of the string, depending on what index arguments you provide. It takes in at least one argument, String.substring(indexStart[, indexEnd])
, the indexEnd
being optional.
So we can subtract the target
length from the str
length, we will get the str
value from the index where we want to compare.
Let's look at that in some code.
function confirmEnding(str, target) {
let strLength = str.length; // 7 since str is "Bastian"
let targetLength = target.length // 1 since target is "n"
return str.substring(strLength - targetLength) // From the 6th index, this method will return "n"
}
confirmEnding("Bastian", "n");
Reference: MDN Documentation - String.prototype.substring()
If the 6th index in "Bastian" is "n", the above will return true
.
str[0] = "B"
str[1] = "a"
str[2] = "s"
str[3] = "t"
str[4] = "i"
str[5] = "a"
str[6] = "n"
Solution
[SPOILER: SOLUTION TO CODE BELOW]
function confirmEnding(str, target) {
return str.substring(str.length - target.length) == target;
}
Links & Resources
Confirm the Ending Challenge on FCC
Thank you for reading!
Top comments (0)