r/programminghorror • • Sep 25 '26

Javascript Rock paper scissors

Post image

I'm way too proud of this solution haha

288 Upvotes

27 comments sorted by

32

u/titanic456 Sep 25 '26

Have you thought about the case, where the user would choose any number larger than 3, or lower than 1?

2

u/No_Opening_5597 Sep 25 '26

its not a proper program haha, I didn't add that in cause I'll probably rewrite this properly, I just thought like "oh wait can I just skip writing any logic and..." the whole joke was the fact that instead of writing it in a sensible way I added a number to a string, the kinda shit you usually do on accident when starting out, and I found it funny as hell that it was running on such janky logic

5

u/Kwolf21 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 26 '26

That's Javascript for ya. Very little rules. 1 + "1" = 11 :)

1

u/gsweats Sep 28 '26

JavaScript was created in a rush and by a newer programmer. Really to bad they didn't see the problem with using + for both addition and concatenation.

113

u/avijt Sep 25 '26

this is outdated , use ai api call instead ,😂😂

43

u/No_Opening_5597 Sep 25 '26

yo who downvoted him i thought it was a fun joke lol

3

u/robin_888 Sep 26 '26

Well, an AJAX call would (usually) be a good way to hide the game logic.

15

u/Winter_Rosa Sep 25 '26

Ahh, concatenation. I probably would've done something similar in C by multiplying comChoice by 10.

Edit, also the real horror here is the lack of input validation. 'A' I choose you!

1

u/No_Opening_5597 Sep 25 '26

haha yeah I didn't add the input validation or anything more because there's no way in hell I'm submitting this as the answer to the exercise I was supposed to do xD

I'll rewrite it cause this is gonna make some people pissed off the way its done, I was supposed to run some type of calculation, create functions and stuff, this is just janky as hell and skips over the actual learning I was supposed to get out of this, but as I was writing the pseudocode and I remember that javascript meme of the guy trying to add 2 + 2 and getting 22, and I couldn't resist building that stuff into a usable thing.

if I was to do something like this properly I would just use a lookup table next time, googled and that's the proper way of doing something like this

56

u/Merry-Lane Sep 25 '26

Musts be like the third "look at my awfully coded shifumi" this week.

8

u/Rigamortus2005 Sep 25 '26

One of the first programs I wrote as a 12 year old kid

2

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 25 '26 edited Sep 25 '26

I'm thinking I like the idea of reading the user input as a number, then multiplying either the user choice or the computer choice by 10, adding them together, and encoding all possible outcomes that way.

Or by 2, but 10 makes the code more readable for us decimal-using humans.

Edit: Actually no, there'd be collisions if we used 2.

3

u/robin_888 Sep 26 '26

I'd let the player input whatever they want, ignore it and just output "You win!", "You lose" or "Draw" with 1/3 probability each.

1

u/RefrigeratorKey8549 Sep 25 '26

3 would work, if the choices are 0 indexed

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 25 '26

3 also works if they are 1 indexed.

1

u/Thenderick Sep 25 '26

Cancels the prompt

1

u/lxe Sep 26 '26

I absolutely love this. The inferred string corrosion. The fully optimized easy to compile single level conditional. This is perfect code.

1

u/Neither_Berry_100 Sep 26 '26

Actually clean. But I would have formatted it better.

1

u/letmehaveanameyoudum Sep 26 '26

why is it in javascript

1

u/razor_train Sep 26 '26

Hey Claude, now install 70 megabytes of styles and fonts.

1

u/born_zynner Sep 28 '26

result = random.choice(["Win","Lose","Tie"])

1

u/SpirouSplade Sep 25 '26

Our first lines of code are the best to remember, I still have my first computer science assignments, I laugh a lot. I didn't know about git at the time, so I have them at my Google Drive folder xD

2

u/csabinho Sep 25 '26

My first lines of code predate Google and Git, but we didn't even learn about SVN, or CVS or whatever, in 5 years of IT school.

1

u/TheLuckyCuber999BACK Sep 25 '26

my first line of code was print("hello, world")

0

u/TheLuckyCuber999BACK Sep 25 '26

j*vascript doing j*vascript things

2

u/HugoNikanor Sep 25 '26

The only JavaScript "weirdness" here is that number + string === string. The actual horror here could be replicated in any language.