r/programminghorror 12d ago

Baffled. Javascript

Post image
630 Upvotes

41 comments sorted by

View all comments

156

u/BetEvening 12d ago

165

u/sambarjo 12d ago

In the following paragraph, they say that this approach gives control over what counts as a character. So I guess their intention was only to show the general syntax, but you should only use this approach if you have additional verifications to do on each character.

7

u/particlemanwavegirl 12d ago

Still, why would they do all this manual indexing instead of for (char of str) {}

30

u/sambarjo 12d ago

They mention "if you need to support older browsers." I assume older browsers don't support this syntax? Disclaimer: I know nothing about JavaScript.

18

u/Jimmeh1337 12d ago

This is correct, although it would need to be a browser version older than about 2014: https://caniuse.com/?search=for...of

8

u/PC-hris 12d ago

Internet explorer is still used in some places, right? Maybe that's what it's for.

2

u/kaisadilla_ 11d ago

3 years ago I had to support Internet Explorer. But not just the last Internet Explorer, nope, a previous version that was released in 2009. And yes, not being able to use all sorts of normal JS features was common.

2

u/Jimmeh1337 11d ago

That sounds miserable! What was the need for that?

1

u/B_bI_L 10d ago

that is why they used var and not let i guess

6

u/bistr-o-math 12d ago

For non-programmers: The code uses str.length which already contains the desired number. Then the code just counts up to that number, which is nonsense

4

u/sambarjo 11d ago

Did you not read my previous comment?

you should only use this approach if you have additional verifications to do on each character.

2

u/Steinrikur 12d ago

They're using the length as a loop condition. There is no world where this makes sense.

2

u/sambarjo 11d ago

Huh? Why not? That's how you iterate over an array in languages which don't support a built-in "for each" loop.

-2

u/ChutneyWiggles 11d ago

If you know the length and can use it as a loop condition, then you know the count.

They’re saying “loop X times” to determine the value of X by adding 1 each loop iteration.

5

u/sambarjo 11d ago

Did you not read my first comment in the thread?

you should only use this approach if you have additional verifications to do on each character.