MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/shittyprogramming/comments/11nkhb0/javascript_is_hard_sometimes/jbs5xfu/?context=3
r/shittyprogramming • u/gabboman • Mar 10 '23
64 comments sorted by
View all comments
Show parent comment
18
This is pretty fucky.
The destructure on the string decomposes it to an object like
{ "0": "h", "1": "e", "2": "l", "3": "l", "4": "o", "5": " ", "6": "w", "7": "o", "8": "r", "9": "l", "10": "d" }
so fetching the keys gets an array of: [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]
[ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]
pop() returns the last item of that array as a string - "10"
pop()
"10"
parseInt("10") returns 10
parseInt("10")
10
adding 1 gives you the length
Legit had to take this piece by piece to see what the hell was going on. Nice one.
View all comments
Show parent comment
18
u/smdaegan Mar 11 '23
This is pretty fucky.
The destructure on the string decomposes it to an object like
{ "0": "h", "1": "e", "2": "l", "3": "l", "4": "o", "5": " ", "6": "w", "7": "o", "8": "r", "9": "l", "10": "d" }so fetching the keys gets an array of:
[ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]pop()returns the last item of that array as a string -"10"parseInt("10")returns10adding 1 gives you the length
Legit had to take this piece by piece to see what the hell was going on. Nice one.