Can you solve this beginner react challenge? - wack-a-mole

Sdílet
Vložit
  • čas přidán 9. 07. 2024
  • mole images:
    web-dev-junkie-files.s3.amazo...
    web-dev-junkie-files.s3.amazo...
    Chapters generated using ytchaptersgenerator.com
    ----------
    00:00 - Introduction
    00:49 - Building the Grid
    02:58 - Randomly Picking Moles
    07:57 - Whacking Moles and Scoring
    11:38 - Hiding Moles
    14:59 - Code Cleanup
    16:00 - Conclusion
    📘 T3 Stack Tutorial: 1017897100294.gumroad.com/l/j...
    🤖 SaaS I'm Building: www.icongeneratorai.com/
    ▶️ Generate Chapters: ytchaptersgenerator.com/
    💬 Discord: / discord
    🔔 Newsletter: newsletter.webdevcody.com/
    📁 GitHub: github.com/webdevcody
    📺 Twitch: / webdevcody
    🤖 Website: webdevcody.com
    🐦 Twitter: / webdevcody

Komentáře • 70

  • @eshw23
    @eshw23 Před 8 měsíci +6

    Love how you started to bring back the challenges

  • @Eldrac
    @Eldrac Před 6 měsíci +1

    Fun problem! Since only one mole is visible at a time the implementation would be much simpler here if the state was typed as number | undefined and just stored the index of the visible mole if one is showing or undefined if no mole is showing

  • @evheniy_danilyuk
    @evheniy_danilyuk Před 8 měsíci +4

    Just stumbled upon your channel, and your videos about refactoring and these type of challenges are pure gold! 🏆Thanks!

  • @MirkoSaugo
    @MirkoSaugo Před 6 měsíci

    I'm a senior dev, but i love to do and watch this kind of challenge!

  • @asagiai4965
    @asagiai4965 Před 8 měsíci +2

    Wow the amount of beginner react challenges here is quite astounding. I just finished the logic on Capital Matching which is a video from last time.
    It works even without hooks, But I probably need to put a hook in it.
    And now we have this.
    I think I might implement an array here. A setInterval function. My initial problem here would be more of the animation side.
    Well good luck to anyone who will try this.

  • @appel-32
    @appel-32 Před 8 měsíci +8

    i remember when you started doing videos learning react, trying to improve your skills. is crazy how confusing effects are, i still don’t really understand them, thankfully i don’t really use them in my projects

    • @WebDevCody
      @WebDevCody  Před 8 měsíci +7

      Luckily react query abstracts a lot of that away, but anything complex requiring timeouts intervals, or web sockets, always confuses me

  • @ibex599
    @ibex599 Před 8 měsíci

    Literally been waiting for a web dev coady challenge video for theee days

  • @Peacemaker.404
    @Peacemaker.404 Před 8 měsíci

    great video and another fun challenge to do!

  • @cod-the-creator
    @cod-the-creator Před 8 měsíci +2

    Great video. It's funny because I do think this qualifies as "beginner" but at the same time if you can do this you can do like 90% of the front-end dev work you'll be asked to do at a real job lol

    • @WebDevCody
      @WebDevCody  Před 8 měsíci +5

      I mean 95% of web dev is show form, validate form, submit form, redirect page, fetch data, show data, repeat

  • @blakeingle8922
    @blakeingle8922 Před 6 měsíci

    The state of your array is derived from some random number. That's why you have so much complexity.
    Just storing `moleIndex` as a random number that changes every second would be better.
    But, if you want to have multiple moles appear at once, then you would want to store a set of mole indices.

  • @zi_t
    @zi_t Před 8 měsíci +1

    14:23 “sometimes i hate react. i think react is the most convoluted thing in the world.” ah, isn’t that comforting to hear

  • @iwantfrens5804
    @iwantfrens5804 Před 8 měsíci

    I cant explain how much I like these videos

  • @stevecrabtree9141
    @stevecrabtree9141 Před 8 měsíci

    Very nice video!

  • @guilhermegirardi
    @guilhermegirardi Před 6 měsíci

    Awesome!

  • @matthewjwhitney
    @matthewjwhitney Před 6 měsíci

    The thing that is most impressive is thst you got dall-e to make teo identical images, but one without the mole...

    • @WebDevCody
      @WebDevCody  Před 6 měsíci +1

      No I use gimp to edit out the mole 😂

    • @matthewjwhitney
      @matthewjwhitney Před 6 měsíci

      @@WebDevCody I wonder what the stats are on who uses gimp because I feel like it's mostly devs fixing shit real quick rather than hitting up the designer haha

  • @jspnser
    @jspnser Před 8 měsíci

    really good one, thanks!

  • @rownhere232
    @rownhere232 Před 8 měsíci

    THANKS FOR A challenge had fun with it

  • @lilpodbebe
    @lilpodbebe Před 8 měsíci +2

    Nice tut! I'd recommend to hold the random index in useState instead of an array of boolean.

  • @user-re8lt2gy3g
    @user-re8lt2gy3g Před 8 měsíci

    Thanks for all the challenges you share 😁🙏 , iwill suggest do like a clone feature challenge from real world applications thanks again

  • @bobbyboxer2664
    @bobbyboxer2664 Před 8 měsíci

    This styles of videos are #chefKiss ❤

  • @Lexaire
    @Lexaire Před 8 měsíci +3

    Great video but would like it if you better explained in detail when and why you need to use callback functions to setState. Also please share final code for these!

  • @TechNikky
    @TechNikky Před 8 měsíci

    14:42 playing game for Bro was harder than making the game

  • @SeibertSwirl
    @SeibertSwirl Před 8 měsíci

    😍😍😍 you’re doing great babe ❤

  • @stakk4
    @stakk4 Před 8 měsíci

    Even following step by step, I'm erroring out on (early in the video line 8 at timestamp 3:45)
    const [moles, setMoles] = useState(new Array(9).fill(true));
    I have an error under the "[]" next to "boolean"
    An element access expression should take an argument.ts(1011)
    Drives me nuts when I do exactly what someone is showing but it doesn't work for me. Anyone have any ideas?

  • @ardianhotii
    @ardianhotii Před 8 měsíci

    Very nice video, and I also noticed the voice change . Did you change the microphone ?

  • @mohammadRizwan-nn5ew
    @mohammadRizwan-nn5ew Před 8 měsíci

    Can we have a challenge for trasnfer list with drag and drop od list items.Also it can handle checked items to drag and drop(reorder)?

  • @Sinke_100
    @Sinke_100 Před 8 měsíci

    If I am more expirianced in react I would probably have stuff to say, but with my current knowledge looks pretty good handling lol
    Also question, u said u used dall-e to make u images, how did you promt it to make you the same one just with the mole?

  • @tomjackson-
    @tomjackson- Před 8 měsíci

    Thanks .

  • @kbkmn
    @kbkmn Před 6 měsíci

    aren't timeout fires even after mole is wacked? you can save timeoutId as positive (visible) mole value instead boolean and call clearTimeout in wacking function

  • @aleksimodebadze1603
    @aleksimodebadze1603 Před 8 měsíci

    Can you do vite ssr tutorial?

  • @wilku04
    @wilku04 Před 8 měsíci

    What is the vs code theme? 😊

  • @personofsomething6205
    @personofsomething6205 Před 8 měsíci

    In 6:00, why did you use the spread operator to assign the array? Why not just make it const newMoles = moles; ?

  • @abdikadirQulle
    @abdikadirQulle Před 8 měsíci

  • @yuuamane2505
    @yuuamane2505 Před 8 měsíci

    How much time under which do you think a beginner needs to be able to complete this? Is under 30 minutes a good time?

    • @WebDevCody
      @WebDevCody  Před 8 měsíci

      I think if you can figure this out in 30 min to an hour, you’re doing good understanding how react works, if it takes longer then there are some fundamentals you’re missing

    • @yuuamane2505
      @yuuamane2505 Před 8 měsíci

      @@WebDevCody Sounds good, thank you!

  • @marcusedberg816
    @marcusedberg816 Před 8 měsíci

    I maybe missed it but it seems that the onClick functionality also is possible when isMole is false which means that you will get a point when you wack the hole.

    • @WebDevCody
      @WebDevCody  Před 8 měsíci +1

      I’d have to check the code, but I thought I check to make sure isMole is true on the index before allowing any of the score logic

    • @marcusedberg816
      @marcusedberg816 Před 8 měsíci

      @@WebDevCody my bad, I see it now

  • @MC-yt8zz
    @MC-yt8zz Před 8 měsíci

    Where can we get the assets please

  • @mihaighise2506
    @mihaighise2506 Před 8 měsíci

    What theme are you using?

    • @WebDevCody
      @WebDevCody  Před 8 měsíci

      Bearded theme stained blue

    • @mihaighise2506
      @mihaighise2506 Před 8 měsíci

      @@WebDevCody Thanks, also keep up the good work!

  • @MoDrazzz
    @MoDrazzz Před 8 měsíci

    Hi, could you post the graphics on some hosting? I don't have dolly-3 unfortunately

  • @IvanRandomDude
    @IvanRandomDude Před 8 měsíci +1

    Improvement idea for readers: consider implementing a dynamic time system instead of a fixed 700ms interval. Initially, you could set a longer duration, say 2 seconds, and gradually reduce this as the game progresses. This accelerating pace will make the game more exciting.

    • @IvanRandomDude
      @IvanRandomDude Před 8 měsíci +1

      Another improvement idea: add animation when mole is whacked instead of it disappearing instantly. This small change will make this game look more professional.

    • @IvanRandomDude
      @IvanRandomDude Před 8 měsíci

      The point being that even mini projects like this allow you to learn and practice a lots different concepts because no matter how simple you start you can always keep adding new features.

    • @asagiai4965
      @asagiai4965 Před 8 měsíci

      that challenge is more up to us than him. he just show the basic so you can have guide (and we should be thankful for that), if you want to create the same game/project.
      btw if you want a dynamic time system
      you probably should have a minimum time and a maximum time.

  • @FaezAnsari-pe2yr
    @FaezAnsari-pe2yr Před 8 měsíci

    Why not instead of having an array of booleans have array of numbers from 1 to 9 and set it as a variable outside of function component and then just have a active state? That would eliminate a lot of bs with arrays and your code would be cleaner and more concise.

    • @WebDevCody
      @WebDevCody  Před 8 měsíci +1

      I wouldn’t store state outside the react component. If you want to make it easier to update the array, you can use immer

    • @FaezAnsari-pe2yr
      @FaezAnsari-pe2yr Před 8 měsíci

      @@WebDevCody The point is the grid doesn't need to be a state and array doesn't need to be updated. You can just have an activeCell state (which is a number) and a grid variable that you store outside of function or inside the function using useMemo.

    • @FaezAnsari-pe2yr
      @FaezAnsari-pe2yr Před 8 měsíci

      here the code:
      function App() {
      const [active, setActive] = useState(null);
      const [score, setScore] = useState(0);
      useEffect(() => {
      const timer = setInterval(() => {
      setActive(Math.ceil(Math.random() * 9));
      setTimeout(() => {
      setActive(null);
      }, 500);
      }, 1000);
      return () => clearInterval(timer);
      }, []);
      const grid = useMemo(
      () =>
      Array(9)
      .fill(1)
      .map((i, x) => i + x),
      [],
      );
      return (
      Score: {score}
      {grid.map((i) => (
      {
      if (active === i) setScore((i) => ++i);
      }}
      className="w-1/3"
      src={active === i ? Wack : Mole}
      />
      ))}

      );
      }

  • @renifer483
    @renifer483 Před 8 měsíci

    no

  • @medAmineRg
    @medAmineRg Před 8 měsíci +1

    i just tried this challenge before watching the video here's my code:
    const [holes, setHoles] = useState([
    "hole.png",
    "hole.png",
    "hole.png",
    "hole.png",
    "hole.png",
    "hole.png",
    "hole.png",
    "hole.png",
    "hole.png",
    ]);
    const [indexTrack, setIndexTrack] = useState();
    const [score, setScore] = useState(0);
    useEffect(() => {
    const interval = setInterval(() => {
    setIndexTrack(() => Math.ceil(Math.random() * 9));
    }, 1000);
    return () => {
    clearInterval(interval);
    };
    }, []);
    return (

    Score {score}

    {holes.map((hole: string, i: number) => (
    {
    if (indexTrack === i) {
    setScore((prevScore) => ++prevScore);
    setIndexTrack(() => undefined);
    }
    }}
    />
    ))}


    );