Basic Algorithm Scripting - Confirm the Ending

Im having some difficulty understanding the syntax of this solution and hoping for some clarity please. I read the relevant link on slice() but still have unanswered questions.

THE TASK: Check if a string (first argument, str) ends with the given target string (second argument, target).

Normally .slice() takes 2 arguments, a startIndex and stopIndex. But if you dont include the stopIndex, then everything gets copied.

This solution appears to copy the entire string then subtracts the target string. In this challenge, my first instinct is that that would mean 7 - 1. Especially since I thought .length always returned an integer.

Additional logic could suggest that this solution means “Bastian” - “n” === “Bastia”. But thats not whats going on here either.

Ive played around with the str and target values so I understand what the solution is doing, it just conflicts with my understanding of the .length syntax.

When does .length not return an integer? Im assuming it has something to do with using “-” with slice(). “-” must not mean subtract here.

function confirmEnding(str, target) {
return str.slice(str.length - target.length) == target;

console.log(confirmEnding("Bastian", "n"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0

Challenge: Basic Algorithm Scripting - Confirm the Ending

Link to the challenge:

1 Like

Again, easiest if you compare to the solution you wrote.

Anyways, looking at MDN

indexStart - The index of the first character to include in the returned substring.

indexEnd - The index of the first character to exclude from the returned substring.

What this says is not quite the same thing as

It is returning an integer.

1 Like

If anyone else is monitoring this and can provide a tangible answer to my questions, I would be much obliged. Thank you.

I provided a “tangible answer”.

.length does in fact return an integer.

Invoking .slice() with a single argument copies everything from the single argument’s value (indexStart) to the end of the string.

That index is the length of the string minus the length of the target.

So, that call to .slice() copies the last characters of str, up to the number of characters in target.

It is easier to explain when you compare with whatever solution you wrote because the quantity str.length - target.length is going to end up showing up in the solution somewhere.

1 Like

I think this is where the confusion is. str.slice(str.length - target.length) isn’t copying the entire string. It’s passing the value of str.length - target.length as the first parameter to the slice call.

That is, confirmEnding("Bastian", "n") would result in str.length being 7, target.length being 1, and the result passed to str.slice() being 6.

str.slice(6) then, would start at index 6 and take the rest of the string. In this case, index 6 would be n, which would match the value of target.


Awesome! This really addressed my confusion and clarified things for me, I appreciate it. Thank you!

1 Like


I just went through this thread and I couldn’t find anything that would require you to respond in this way.
“Harrassment” as a word has a lot of weight to it and calling a moderater who’s been helping people on the forums for years is really inappropriate and doing this on a public thread that anyone can see is not something you should do “just because”.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.