-3

I'm trying to solve the following problem:

Complete the solution so that it returns true if the first argument (string) passed in ends with the 2nd argument (also a string).

Examples:

Solution ('abc', 'bc') // returns true, solution ('abc', 'd') // returns false Create a function that does this.

My code is:

function solution(str, ending){
 return (str.split('').filter(str => str.length > str.length - 2).join('')) == ending ? "true" : "false"
 
 
}

The error I get:

expected 'false' to equal true
Dugnom
  • 342
  • 1
  • 5
  • 12
Emma Maina
  • 35
  • 8
  • That's a complicated effort when there is just [endsWith](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith). Any reason why you don't use that method? – trincot Jul 28 '22 at 08:06
  • How about using endsWith? str.endsWith(ending) – Dr4jw3r Jul 28 '22 at 08:06
  • no particular reason just wanted to see if i could use this way or if a few tweaks can be done so that it can work – Emma Maina Jul 28 '22 at 08:08
  • 1
    What do you think `str.length > str.length - 2` does? Do you see **any** possibility for that expression to be false? And why 2? What is the relevance of 2 in this problem? – trincot Jul 28 '22 at 08:09
  • Does this answer your question? [How to implement String.endsWith() without using built-in method .endsWith() in Java](https://stackoverflow.com/questions/16026898/how-to-implement-string-endswith-without-using-built-in-method-endswith-in) – Dugnom Jul 28 '22 at 08:17

2 Answers2

1

Some issues:

  • You have used the name str for the function parameter and for the parameter in the filter callback function. So in that callback function you'll have no access to the original str value.
  • In the filter callback, str will be a single character, so its length will always be 1. It makes no sense to compare it with anything
  • If you go with filter, then you'd want to do something with the index of the character, not the character itself.
  • In this problem the value 2 has no special meaning, it should not need to occur anywhere in your code
  • The function returns a string "true" or "false", but you are required to return a boolean value (true or false). Those are not strings.
  • Using the conditional (ternary) operator just to return a boolean is overkill: just return the boolean expression that serves for the condition.
  • The whole idea of using filter is not necessary as there is endsWith

I guess you wanted to do something like this:

function solution(str, ending){
  return str.split('').filter((_, i) => i >= str.length - ending.length).join('') == ending ? true : false;
}

console.log(solution("longword", "word"));

But it can be as simple as:

function solution(str, ending){
  return str.endsWith(ending);
}

console.log(solution("longword", "word"));
trincot
  • 317,000
  • 35
  • 244
  • 286
  • please elaborate on this part .filter ((_, i) => i >= str.length - ending.length) – Emma Maina Jul 28 '22 at 08:49
  • and also is this the best practice? – Emma Maina Jul 28 '22 at 08:51
  • `i` is the index of the current visited character in the loop, and `str.length - ending.length` is the number of characters that should be *excluded*. So if the current index has passed over the number of characters to exclude, the expression will return true, and so `filter` will *include* that character. – trincot Jul 28 '22 at 09:09
  • This was what I *guessed* you tried to do. And no, this is not best practice. The advised way is `endsWith`. – trincot Jul 28 '22 at 09:09
0

How about this

function solution(str, ending){
  var a = str.slice(-3);
  var b = ending;
  if (a === b){
    return true;
  }
  
  else
    return false;
}
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 14 '23 at 13:10