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.
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.
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.
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”.