3

This task requires that you write a function that takes two arguments. The first argument is a string called str and the second argument is a string that our target ending named target. The task is to verify that the ending of str is identical to the target ending. The instructions indicate to use the .substr() method to compare the endings to the targets. The problem I have is that there are going to be multiple starting points and length arguments for the .substr method since the target endings can be of variable length. Take a look at my attempt to solve this issue and please direct me in the right path.

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');
Austin Hansen
  • 364
  • 1
  • 6
  • 17
  • 1
    Your code should work fine if `var start = str.length - target.length;` – MayThrow Jun 28 '15 at 02:48
  • possible duplicate of [endsWith in javascript](http://stackoverflow.com/questions/280634/endswith-in-javascript) –  Jul 05 '15 at 07:33
  • @torazaburo is not a DUP: the user asked _The instructions indicate to use the .substr() method to compare the endings to the targets_ ... this questios is not about endWidth. – rnrneverdies Jul 05 '15 at 08:19
  • @oneway Well, it's a matter of how narrowly one interprets the question. The question has a broader context of how to do the equivalent of `endsWith`; it has a narrower context of how to do that in one particular, **suboptimal** way. It's well within the scope of legitimate answers, IMHO, to suggest alternative, better approaches to solve the underlying question. A question **is** a duplicate if it asks the same basic question, especially when, as in this case, the question suggested as a duplicate **does** provide an answer giving a correct implementation based on `substr`. –  Jul 05 '15 at 09:12

6 Answers6

6

EDIT

As @torazaburo said. The correct answer Is:

function end(str, target) {
    return target === str.substr(str.length - target.length);
}

Because The string does end with a null string

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 is setting up a logical comparison using the && operator. The left side target.length > 0 should always return true with a valid target input. The left side is setting target equal to the substr starting at the point located by taking the str.length (the position at the far right of the str) and subtracting the target length (to arrive at the start point of our substring). There is no need for an end point input because the substring will run to the end of str.

rnrneverdies
  • 15,243
  • 9
  • 65
  • 95
  • 1
    This is the correct answer. It would help for a newcomer to have an explanation so let me take a shot. This code is setting up a logical comparison using the '&&' operator. The left side 'target.length > 0' should always return 'true' with a valid 'target' input. The left side is setting target equal to the 'substr' starting at the point located by taking the 'str.length' (the position at the far right of the 'str') and subtracting the 'target' length (to arrive at the start point of our substring). There is no need for an en point input because the substring will run to the end of the str. – Austin Hansen Jun 28 '15 at 02:40
  • Why is `target.length > 0` necessary? –  Jul 05 '15 at 07:19
  • Case 4 should return a true. The string **does** end with a null string. –  Jul 05 '15 at 07:35
1

Here is a easy 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;
}
squal
  • 185
  • 3
  • 14
  • Using an if to store a boolean in a variable declared in the header - is there a more convoluted way to write `return last === target`? – Moritz Ringler May 17 '23 at 06:33
0
function end(str, target) {
  var start = str.length - target.length;

  if(str.substr(start) == target){
    return true;
  }
  else {
    return false;
  }
}

You can also try this code.

0

The substring method can take a negative value 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
som
  • 2,023
  • 30
  • 37
0

Here you go:

const solution = (str, target) => str.endsWith(target);

The cleanest way.

Tosha
  • 1
0

function confirmEnding(str, target) {
  let ending = str.slice(str.length-target.length)
  return ending == target
}

console.log(confirmEnding("Congratulation", "on"))
siftanzil
  • 1
  • 2