Tuesday, March 1, 2016

CONFIRM THE ENDING

Challenge here was to check if a string (first argument) ends with the given target string (second argument).

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 where strLength is the length of the string (for example, if start is -3 it is treated as strLength - 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