MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/shittyprogramming/comments/11nkhb0/javascript_is_hard_sometimes/jbqanqg/?context=3
r/shittyprogramming • u/gabboman • Mar 10 '23
64 comments sorted by
View all comments
Show parent comment
35
Definitely pulled their punches, then. You could totally abuse .pop() to get the length if you wanted to be a jerk.
.pop()
1+parseInt(Object.keys({..."hello world"}).pop())
Nobody expects pop to work unless they've played with it.
pop
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") returns 10adding 1 gives you the lengthLegit had to take this piece by piece to see what the hell was going on. Nice one. 2 u/pacanukeha Mar 12 '23 let a = 0;let b = "helloworld".split('');while (b.pop()){a += 1;}console.log(a);
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.
2
let a = 0;let b = "helloworld".split('');while (b.pop()){a += 1;}console.log(a);
let a = 0;
let b = "helloworld".split('');
while (b.pop()){
a += 1;
}
console.log(a);
View all comments
Show parent comment
35
u/novagenesis Mar 10 '23 edited Mar 10 '23
Definitely pulled their punches, then. You could totally abuse
.pop()to get the length if you wanted to be a jerk.Nobody expects
popto work unless they've played with it.