# 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"));
``````

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

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.

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

`.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 `str`ing 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`.

2 Likes

What is the problem here? You are receiving very good help from someone who is highly qualified to give the help. Is there something I am missing here that would cause you to make such a request?

1 Like

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

1 Like

Hey!

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â€ť.

5 Likes