Confirm ending - please help to understand why my code does not pass

Tell us what’s happening:
I am struggling here with the simple task where I should Check if a string (first argument, str ) ends with the given target string (second argument, target ).

I came with this code so far, but for some reason it does not work, although let ending = str.slice(-target.length) matches the target argument when I check it with the console.log. Please tell me what do I do wrong?
Thank you very much in advance!

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

confirmEnding("Bastian", "n");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36.

Challenge: Confirm the Ending

Link to the challenge:

welcome to the forum @bkuberlinov!
yes, you’ve done everything right, but notice

it should return true and not str, to make the code look better and avoid mistakes like this replace this:

with this:

return ending === target
1 Like

and for even more efficiency and a better look you can write

const confirmEnding = (str, target) => str.slice(-target.length) === target

Thank you very much for your answer. I realized that I understood the task wrong, I thought that I should return the string and not just to check if they match or not.
Sorry for a stupid question, but I still don’t understand and I really would like to know, why can’t I return the string, but only true or false in that case?
Out of personal interest, I am now trying to figure out how would I return the string instead of true or false.

the challenge asks you to figure out if a string ends with a given sub-string, think of it like a question:
“does this sentence end with those letters?”
the answer would be “yes” or “no”, answering with “cat” will be just stupid.
also returning the string without any modifications is kinda pointless, because the string is a user input (or a level in a process of the input) and is already known to the user, the whole point of a function is returning something different and based, take for example

function func(a)  {
 return a

this function is useless, but sometimes you would return the input unmodified if it meets a criteria.
I might have confused you, but basically that’s what the challenge asks you to do, as a developer your work will be establishing solutions for challenges, depending on the challenges

1 Like

And again thank you for your answer. It helped a lot for a better understanding of a way of thinking needed.

1 Like

could you mark my reply as a solution please? I want to level up trust levels and those markings help a lot (there’s a green button near the reply to mark as solution)