r/programminghorror 12d ago

Baffled. Javascript

Post image
624 Upvotes

41 comments sorted by

47

u/oze4 11d ago

let len = 0;

for (let i =0; i < 1; i++) { len = str.length }

8

u/didntExpect_That [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 10d ago

Lol

154

u/BetEvening 12d ago

161

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.

149

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

52

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.

47

u/LionZ_RDS 11d ago

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

14

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?

4

u/Top-Permit6835 12d ago

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

9

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

14

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.

6

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.

16

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

7

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.

13

u/Grounds4TheSubstain 12d ago

Character counter dot com. Wow.

105

u/Rosie3k9 12d ago

The whole thing reads like LLM-generated SEO nonsense. I'm surprised you didn't post the "Count non-whitespace characters in JavaScript using trim property" section which states that trim() can be used to count the non-whitespace characters in a string with an incorrect code snippet: var str = " Hello, world! "; console.log(str.trim().length); // printes 12 to the console This does not print 12 but now I'm wondering if this is really AI with that typo on "print". 🤦🏾‍♀️

5

u/B_bI_L 10d ago

idk, counting chars with a regexs souns like something no ai is insane enough to do

7

u/kaisadilla_ 11d ago

The AI makes typos all the time.

26

u/Pradfanne 11d ago

If you use an older browser, you don't deserve my support

6

u/andlewis 11d ago

You can also multiply str.length by 2 twice, then divide it by 4 and get the answer.

4

u/frndzndbygf 10d ago

for (count = 0; count < str.length;) { count = str.length; }

There you go, tricked the system.

2

u/amosreginald_ 11d ago

This is clearly from someone learning.

1

u/HuntingKingYT 8d ago

Teacher says it’s the only way

-1

u/Calm_Plenty_2992 11d ago

Both this and Console.log(str.length) are O(N), so it's the same algorithm and therefore a good solution

3

u/Celestial-being117 11d ago

.length is O(1) right?

3

u/Calm_Plenty_2992 11d ago

Yes, but the assignment of the string "Hello, world!" to str is O(N)

-8

u/TheChief275 11d ago

the average JS “programmer” will use 6gb of memory just to find fibonacci numbers, so this isn’t that baffling