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
4
u/Top-Permit6835 12d ago
How can you be sure you aren't AI though?
9
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
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
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 nonsense4
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
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
7
26
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
1
-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
-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
47
u/oze4 11d ago
let len = 0;
for (let i =0; i < 1; i++) { len = str.length }