Lesson 22: correct is wrong?


#1

I'm stuck at lesson 22 about substrings.

Exercise:
Find the 4th up to and including the 7th letter of the string "wonderful day".

Error:
Oops, try again. Make sure to use the .substring() function in your code!

Code:

"wonderful day".substring (3, 7);

Why is this?


#2

Update: I figured it out already.
Apparently, this site hates it when I put a space between "substring" and "(3, 7)".


#3

"wonderful day".substring (3, 7);
check space on substring


#4

Not just this site, you would receive that error anywhere. Putting a space there is improper syntax.


#5

I see now.
I personally never liked Javascript, but work wants me to learn it, so I have to now.

Thanks for the explanation guys! (^ν^)


#6

Putting a space there (or really anywhere between two tokens) is perfectly fine syntax (relevant section of the standard).

It's really just the site's solution checker that's having the problem here.


#7

While yes, whitespace would not impact certain languages, it does indeed impact languages such as JavaScript and Python. Incorrect use of Indentation and whitespace can absolutely destroy programs written in those languages.


#8

Yes, it does.

Anyway, if you don't want to take the standard's word for it, you can just test it in Node, your browser or any JavaScript online REPL you can find. The code works fine with a space in there.


#9

My apologies, I tried the link again and it took me to the proper place. I must have done something weird, because the first time I clicked it, it took me to a random page on CNN... :dizzy_face:

Anyway... odd hyperlink glitches aside, I apparently misunderstood what you were saying. Yes, indeed, a white space would work in this one instance. There are however many other instances where it will not work, both aesthetically and practically. Codecademy is merely trying to reinforce the idea of not using white spaces where they don't need to be.


#10

There are however many other instances where it will not work, both aesthetically and practically.

I don't know about aesthetically, but practically the only place where you can't just add whitespace would be in the middle of a token.

Codecademy is merely trying to reinforce the idea of not using white spaces where they don't need to be.

Honestly I find it much more likely that the script simply uses a too-simple regex that fails on whitespace. If it realized that the code is correct, but rejected it for style reasons, it would say so instead of falsely claiming that the function is not called.


#11

While I agree with this statement

I don't necessarily agree with you here...


#12

Maybe I misunderstood what you meant by "practically". What I meant is that you can add spaces between any two tokens in JavaScript without changing the behaviour of the program in any way (even if it may look funny).

I'm 99% sure that this statement is true (and if you know a counter-example, feel free to post it) and it's also what the previously linked of the standard says.


#13

One word...

Indentation :wink:


Edit: I'm such an idiot... the whitespace/indentation rules I am thinking of apply to Python, not JavaScript. I'm just so adjusted to Python that, no matter the language I'm using, I always try to utilize it's rules on spacing and indentation. Now, If you'll excuse me, I'm going to go bang my head against a wall a few hundred times. :sweat:


#14

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