Apologize if this is in the wrong category. Was unsure if it fit under the help or javascript section. Appreciate anyone who replies.
The context:
So this is not so much a question for help as it is a question about function.
When finishing every task I usually take a look at the Get a Hint section to find out how the task was intended to be solved. The hint page says to use the str.slice() function, and my question is this:
- Are there any notible differences in efficiency between the slice() and substring() methods?
- Is there a right and wrong way to solve it?
My thought process and code is added below.
My code
function confirmEnding(str, target) {
// since the target can be an arbritrary length we first need to find out how long the target is
// then we need to find out how long the str is
// subtract the target length from the string length to find where the substring should start.
// we must then compare the ending of the substring with the target and return true or false.
return (str.substring((str.length-target.length)) === target);
}
confirmEnding("Bastian", "n");
Link to the challenge:
Confirm the Ending task
Hint Page for Confirm the Ending