+ 4

[Javascript] substr vs. substring FCC challenge [CLOSED]

So, is it FCC too hard and SL too easy? Can't get the instructions for this challenge from FCC: qte 1. Check if a string (first argument, str) ends with the given target string (second argument, target). 2. The function is a whole Boolean operation. You need to return true if the first argument ends with the second argument. 3. This means that for the problem script, it should return true for the confirmEnding('Bastian', 'n'); case. Take a look at how substr() works. You will be trying to get the last

31st Aug 2017, 2:18 AM
Kustaa
10 Answers
+ 10
I guess that the idea is to compare the n last characters of first argument and the whole second, where 'n' is the length of the second... So, you can consider that you have to sub-string from the -n index ("turn it into a negative number"); but however you cannot use it as is in JS, so concretely, you will calculate the positive 'real' index, let say m = first_arg.length - n ^^
30th Aug 2017, 9:05 PM
visph
visph - avatar
+ 6
What suggest @Kirk Schafer, is that I forgotten that substr() and substring() methods accept negative indexes, unlike direct access (by targeting an item by its index inside squared brackets) ;)
30th Aug 2017, 9:15 PM
visph
visph - avatar
+ 6
Bye
31st Aug 2017, 2:45 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
Just linking to the official documentation of how substr() works reveals "a line of code", so I'm copying from the docs. You've listed #3 twice, so let's say...3a and 3b. It's necessary to review 3b to understand 4. Omitting the code sentence from substr and substring's descriptions, maybe this will help when looking at 4 again: editing in*: substr: Parameter 1: index: Location at which to begin extracting characters. If a negative number is given, it is treated as strLength-start where strLength is the length of the string. For example <omittted> substring: Parameter 1: indexStart: An integer between 0 and the length of the string, specifying the offset into the string of the first character to include... * Sorry for the delay. Not being able to link was a surprise problem (Mozilla disables copy+paste and I tried to split my screen to retype, causing me to crash several times and lose this answer).
30th Aug 2017, 9:11 PM
Kirk Schafer
Kirk Schafer - avatar
+ 5
@visph I edited bigger/up (from a few lines) since the question didn't want "any line of code". Made my answer messier and disconnected yours a little :/ (sorry about that)
30th Aug 2017, 9:35 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
Kirk and visph, you guys created great applications. I salute your knowledge and skills.
31st Aug 2017, 12:44 AM
Kustaa
+ 2
Thank you for your comments.
31st Aug 2017, 12:07 AM
Kustaa
+ 2
@visph, your explanation guided me enough to solve it. Can't thank you enough.
31st Aug 2017, 1:20 AM
Kustaa
+ 2
vengat, what do you mean gave up on xp.
31st Aug 2017, 1:51 AM
Kustaa
+ 2
Well. I agree that he might don't care about XP. I don't believe he "gave them" for answer me (or others), though. I don't "give" mine for supporting you, for instance. And I do. I also stay in SL supporting with what I have= I learn. You answer questions too. Some day I'll also. When I have enough knowledge to give. There wouldn't be SL if all were like vipsh, or all were like vengat. Imagine if all were like me... geez. Nah. I get your point, though. Sorry the rambling. It's too late here. Bye.
31st Aug 2017, 2:32 AM
Kustaa