Challenge here was to check if a string (first argument) ends with the given target string (second argument).
With some helpful link as below:
The total concentration should be on the above-mentioned function(link above) to efficiently solve the problem.
Full syntax is
str.substr(start[, length])
start
- Location at which to begin extracting characters. If a negative number is given, it is treated as
strLength + start
wherestrLength
is the length of the string (for example, ifstart
is -3 it is treated asstrLength - 3
.) length
- Optional. The number of characters to extract.
- The problem was straight forward, my solution is as below:-
function end(str, target) {
var x = str.substr(-target.length , target.length);
if (x === target){
return true;
} else {
return false;
}
}
No comments:
Post a Comment