Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted over 1 year ago

Rock, Paper, Scissors | (NextJS,TailwindCSS,Framer Motion)

framer-motion, react, tailwind-css, next
Emre Kalfa•180
@rustysym
A solution to the Rock, Paper, Scissors game challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


Hey! I'm Emre and this is my Rock,Paper,Scissors project

There are some problems with my project

  • Score doesn't seem to be updating correctly
  • Score takes negative values

I think the main problem is due to the use of useEffect

Any suggestions on how I can improve are welcome

GameContext.js

const onSelect = () => {
    const userSelect = GameRules[selection].value;
    const number = Math.floor(Math.random() * 3);
    setHouseSelection(number);
    setTimeout(() => {
      setShow(true);
      if (GameRules[houseSelection].beats.includes(userSelect)) {
        setResult("You Lose");
        setScore((score) => score - 1);
      } else {
        if (GameRules[houseSelection].value == userSelect) {
          setResult("Tie");
        } else {
          setResult("You Win");
          setScore((score) => score + 1);
        }
      }
    }, 3000);
  };

containers\game\index.js

const { onSelect, houseSelection, result, show, playAgain } =
    useContext(GameContext);
useEffect(() => {
      onSelect();
  }, [houseSelection]);
Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Kyle Johnson•250
    @11kyle
    Posted over 1 year ago

    Hey Emre! Great job on your solution! I checked into your question and have some feedback.

    • Score takes a negative value.

    This can be fixed by wrapping your setScore((score) => score -1) inside an if statement like so

    if (score > 0) {
      setScore((score) => score - 1)
    }
    
    • Score doesn't seem to be updating correctly

    You are right on thinking it is the useEffect hook that is causing problems here. useEffect gets called on initial render and again every time anything in the dependency array changes. You also never want to update anything from the dependency array from inside the useEffect. This could cause an infinite loop. In your case, the useEffect is running a minimum of 2 times and that is part of why you are seeing weird scores. The way your game is setup you can actually leave the dependency array empty like so

     useEffect(() => {
       onSelect();
     }, []);
    

    In addition, there's a weird thing happening with useState inside your GameContext.js. useState actually takes 'time' to update its value. It's just a part of react. When you setState and then ask your function to do something with it, it is using the previous value. You can replace houseSelection with number inside your function like so

    const onSelect = () => {
      const userSelect = GameRules[selection].value;
      const number = Math.floor(Math.random() * 3);
      setHouseSelection(number);
    
      setTimeout(() => {
        setShow(true);
        if (GameRules[number].beats.includes(userSelect)) {
          setResult("You Lose");
          setScore((score) => score - 1);
        } else {
          if (GameRules[number].value == userSelect) {
            setResult("Tie");
          } else {
            setResult("You Win");
            setScore((score) => score + 1);
          }
        }
      }, 3000);
    };
    
    Marked as helpful

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord
Frontend Mentor logo

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit 1st-party linked stylesheets, and styles within <style> tags.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub