Confirm line termination (by changing the final length)
This task requires that you write a function that takes two arguments. The first argument is a string with a name str
, and the second argument is a string that ends with our target with a name target
. The challenge is to check that the end str
matches the end of the target. The instructions show how to use the method .substr()
to compare endings to targets. The problem is that .substr
multiple start and long arguments will be used for the method , since the final end values ββcan be of variable length. Take a look at my attempt to resolve this issue and please guide me in the right direction.
function end(str, target) {
var start = str.length - (target.length - 1);
if(str.substr(start, str.length) == target){
return true;
} else {
return false;
}
}
end('Bastian', 'n');
source to share
EDIT
As @torazaburo said. Correct answer:
function end(str, target) {
return target === str.substr(str.length - target.length);
}
Since string is null- terminated
ORIGINAL ANSWER
function end(str, target) {
return target.length > 0 && target === str.substr(str.length - target.length);
}
http://jsfiddle.net/tqsx0gLa/2/
From comments:
This code sets up a boolean comparison using an operator &&
. The left side target.length > 0
must always return true
with valid input target
. The left side sets the target to be equal substr
by starting at the point located with str.length
(position at the far right corner str
) and subtracting the length target
(to get to the starting point of our substring). There is no need to enter an endpoint because the substring will work to the end str
.
source to share
Here's a simple solution:
function confirmEnding(str, target) {
var result;
//getting the last string depend on target length
var last = str.substring(str.length - target.length);
//checking the last string with the target
if(last === target){
result = true;
} else {
result = false;
}
return result;
}
source to share
The substring method can be negative to work from the end of the string. The solution to your problem is very simple:
function end (str, target) {
return str.substr(-target.length) === target;
}
end("simple is better", "better"); // returns true
// which is the same as writing
"simple is better".substr(-6) === "better" // true again
source to share
I love your original answer, it is clean and easy to prepare. Try removing -1 on line 2. That way it will return all target words in your substring.
function end(str, target) {
var start = str.length - (target.length);
if(str.substr(start) == target){
return true;
} else {
return false;
}
}
end ("He has to give me a new name", "name")
If the substring does not return a second argument, it will return from start to the end of the string.
source to share
function confirmEnding(str, target) {
var position = str.length - target.length; //get start position for our .substr method.....
if (str.substr(position, target.length == target){ // logical expression wich tell our method(.substr) from which position we need get information then we compare result with our "target" argument.
return true;
} else { return false; }
}
confirmEnding ("Bastian", "n"); // true