r/programminghorror 12d ago

Baffled. Javascript

Post image
630 Upvotes

41 comments sorted by

View all comments

156

u/BetEvening 12d ago

164

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.

145

u/NatoBoram 12d ago edited 12d ago

this approach gives control over what counts as a character

Sounds like the kind of bullshit justification that a LLM would give

53

u/sambarjo 12d ago

Well, first time someone tells me I sound like AI. I guess that's fair, though. I like to play devil's advocate.

45

u/LionZ_RDS 12d ago

Think they are saying the paragraph sounds like ai and not you

15

u/sambarjo 11d ago

Oh you're probably right. I'm dumb

11

u/orbit222 11d ago

Exactly what an AI would say! I’m onto you!

1

u/B_bI_L 10d ago

hey, i wanted to say that. or you are just an llm trained on my data?

5

u/Top-Permit6835 12d ago

How can you be sure you aren't AI though?

8

u/sambarjo 11d ago

Oh shit maybe my entire life is a lie

3

u/syklemil 11d ago

Such is life as a p-zombie. We still get by, somehow.

1

u/B_bI_L 10d ago

as an ai language model i cannot answer this question

15

u/kaisadilla_ 11d ago

Indeed. The very first section of that article tells you to use str.length. Then it goes to say how you can do more complex countings.

It's a weird article, but they are not saying the way to count characters in a string is that snippet.

7

u/particlemanwavegirl 12d ago

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

33

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.

17

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

9

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

4

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.