Struggling with My Rock, Paper, Scissors Game in JavaScript

0
7
Asked By CuriousCoder42 On

Hey there! I'm diving into JavaScript and currently stuck on an assignment from The Odin Project where I'm building a Rock, Paper, Scissors game. I can get the prompt to ask me for my choice, but after that, when I enter my input, the function doesn't seem to run as expected. I can't figure out what I messed up in my functions and could really use some guidance to fix my code. Thanks a ton! Here's the project link for reference.

3 Answers

Answered By CodeGuru99 On

The others did a great job explaining your specific issues, but I'd like to help you with a fundamental concept. It’s important to understand the difference between what a function returns and its side effects.

When you return a value using `return`, for example, `return "Example text";`, the function provides that value when called. But using `console.log("Example text")` will log that text to the console as a side effect; it doesn't return it. Concentrating on this distinction might really help you avoid similar pitfalls in the future.

EagerLearner11 -

Absolutely! I’m definitely going to revisit some articles and videos on functions.

Answered By HelpfulHarry88 On

Hey! You're really close! I spotted a couple of small bugs in your code:

1. Your `getHumanChoice()` function doesn’t actually return anything. You log the choice, but without a return statement, `humanChoice2` ends up as undefined. Just add `return humanChoice;` after your console.log line.

2. In `getComputerChoice()`, you’re returning strings like "Computer chose rock", but in your `playRound()`, you're comparing them against just "rock". They won't match! Change those returns to just "rock", "paper", or "scissors" without the extra text.

Try making those updates, and good luck!

NewbieNina -

Thank you! I had a feeling it was an issue with the strings, but couldn't put my finger on it. Now everything's running fine, but the console log says `undefined` after `console.log(playRound(humanChoice2, computerChoice));`. Is that normal?

Answered By CodingConnoisseur22 On

Another user already pointed out those bugs, so I won’t repeat them. But once you’ve fixed your issues, you might want to check out an implementation I did that approaches the game in a different way. It’s a data-driven version to avoid branches for determining the winner. Just a side note, it’s not a direct fix for your code, but could be interesting! There are countless ways to tackle a problem in programming, and exploring them can really boost your understanding.

GratefulGamer -

That sounds great! Your method seems shorter and more efficient. I’ll definitely check it out. I was also considering arrays but I stuck to this method for some reason. Thanks again!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.