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

Поділитися
Вставка
  • Опубліковано 5 вер 2024

КОМЕНТАРІ • 71

  • @eshw23
    @eshw23 10 місяців тому +6

    Love how you started to bring back the challenges

  • @Eldrac
    @Eldrac 8 місяців тому +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

  • @appel-32
    @appel-32 10 місяців тому +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  10 місяців тому +8

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

  • @evheniydan
    @evheniydan 10 місяців тому +4

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

  • @asagiai4965
    @asagiai4965 10 місяців тому +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.

  • @cod-the-creator
    @cod-the-creator 10 місяців тому +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  10 місяців тому +5

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

  • @zi_t
    @zi_t 10 місяців тому +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

  • @MirkoSaugo
    @MirkoSaugo 8 місяців тому

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

  • @blakeingle8922
    @blakeingle8922 8 місяців тому

    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.

  • @lilpodbebe
    @lilpodbebe 10 місяців тому +2

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

    • @WebDevCody
      @WebDevCody  10 місяців тому

      Yeah that could work as well

  • @ibex599
    @ibex599 10 місяців тому

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

  • @Lexaire
    @Lexaire 10 місяців тому +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!

  • @matthewjwhitney
    @matthewjwhitney 8 місяців тому

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

    • @WebDevCody
      @WebDevCody  8 місяців тому +1

      No I use gimp to edit out the mole 😂

    • @matthewjwhitney
      @matthewjwhitney 8 місяців тому

      @@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

  • @iwantfrens5804
    @iwantfrens5804 10 місяців тому

    I cant explain how much I like these videos

  • @guilhermegirardi
    @guilhermegirardi 8 місяців тому

    Awesome!

  • @Peacemaker.404
    @Peacemaker.404 10 місяців тому

    great video and another fun challenge to do!

  • @user-re8lt2gy3g
    @user-re8lt2gy3g 10 місяців тому

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

  • @rownhere232
    @rownhere232 10 місяців тому

    THANKS FOR A challenge had fun with it

  • @BiswadeepChakraborty007
    @BiswadeepChakraborty007 Місяць тому

    Keep up the good work. Just one query, I tried to get the timeId for the setTimeout function and then used the useEffect cleaning function to clean that out. But it didn't work out. Here is what I am doing
    let timeId;
    const timeInterVal = setInterval(() => {
    const randomIndex = Math.floor(Math.random() * moles.length);
    popMole(randomIndex);
    timeId = setTimeout(() => {
    hideMole(randomIndex);
    }, 700);
    }, 1000);
    return () => {
    clearInterval(timeInterVal);
    clearTimeout(timeId)
    };
    Can you please check and let me know where I am going wrong.

  • @TechNikky
    @TechNikky 10 місяців тому

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

  • @stevecrabtree9141
    @stevecrabtree9141 10 місяців тому

    Very nice video!

  • @kbkmn
    @kbkmn 8 місяців тому

    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

  • @bobbyboxer2664
    @bobbyboxer2664 10 місяців тому

    This styles of videos are #chefKiss ❤

  • @mohammadRizwan-nn5ew
    @mohammadRizwan-nn5ew 10 місяців тому

    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)?

  • @jspnser
    @jspnser 10 місяців тому

    really good one, thanks!

  • @marcusedberg816
    @marcusedberg816 10 місяців тому

    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  10 місяців тому +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 10 місяців тому

      @@WebDevCody my bad, I see it now

  • @Sinke_100
    @Sinke_100 10 місяців тому

    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?

  • @ardianhotii
    @ardianhotii 10 місяців тому

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

    • @WebDevCody
      @WebDevCody  10 місяців тому

      I’m sick, I have a cold

  • @tomjackson-
    @tomjackson- 10 місяців тому

    Thanks .

  • @aleksimodebadze1603
    @aleksimodebadze1603 10 місяців тому

    Can you do vite ssr tutorial?

  • @stakk4
    @stakk4 10 місяців тому

    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?

  • @IvanRandomDude
    @IvanRandomDude 10 місяців тому +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 10 місяців тому +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 10 місяців тому

      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 10 місяців тому

      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.

  • @SeibertSwirl
    @SeibertSwirl 10 місяців тому

    😍😍😍 you’re doing great babe ❤

  • @wilku04
    @wilku04 10 місяців тому

    What is the vs code theme? 😊

  • @yuuamane2505
    @yuuamane2505 10 місяців тому

    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  10 місяців тому

      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 10 місяців тому

      @@WebDevCody Sounds good, thank you!

  • @medAmineRg
    @medAmineRg 10 місяців тому +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);
    }
    }}
    />
    ))}


    );

  • @personofsomething6205
    @personofsomething6205 10 місяців тому

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

    • @WebDevCody
      @WebDevCody  10 місяців тому +1

      You need a new array reference or else react won’t rerender

    • @personofsomething6205
      @personofsomething6205 10 місяців тому

      Thanks mate, didn't know that.

  • @abdikadirQulle
    @abdikadirQulle 10 місяців тому

  • @FaezAnsari-pe2yr
    @FaezAnsari-pe2yr 10 місяців тому

    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  10 місяців тому +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 10 місяців тому

      @@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 10 місяців тому

      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}
      />
      ))}

      );
      }

  • @MC-yt8zz
    @MC-yt8zz 10 місяців тому

    Where can we get the assets please

    • @WebDevCody
      @WebDevCody  10 місяців тому

      Should be in description

  • @mihaighise2506
    @mihaighise2506 10 місяців тому

    What theme are you using?

    • @WebDevCody
      @WebDevCody  10 місяців тому

      Bearded theme stained blue

    • @mihaighise2506
      @mihaighise2506 10 місяців тому

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

  • @MoDrazzz
    @MoDrazzz 10 місяців тому

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

  • @renifer483
    @renifer483 10 місяців тому

    no