Mole Unearther, Task: 26, additional practice help: #1. Additonal points for reaction time

Project in question(https://www.codecademy.com/paths/create-video-games-with-phaser/tracks/game-dev-learn-javascript-function-and-scope/modules/game-dev-project-functions-and-scope/projects/mole-unearther)

I have semi-succeed in this task but not to a satisfying degree. It may be purely based on where I tried to place the code, but seeing as the previous lessons were about scope pollution I was trying to build it into what was there. My idea was:

  1. Create a global variable called reactionTimer and initialize it to 0.

  2. The lesson previously had you create a function to update timeLeft so it counted down from 30. I figured inside this same function I could also have it count up for reactionTimer.

//Very top of .js code
let timeLeft = 30
let reactionTimer = 0
/*
whole bunch of other code
*/
   const updateTimer = () => {
      timeLeft --
      reactionTimer ++
    }
  1. From here it “seemed” simple enough. Just create a switch statement where the lesson made you add points for hitting the mole. Using the function updateScore() that we also had previously created. Then compare if 1, 2, or 3 seconds had passed then resetting the reactionTimer variable to 0 so it could count back up.
const applyHitReward = () => {
      updateScore(5)
      switch (reactionTimer) {
        case (0):
          updateScore(3)
          reactionTimer = 0
          break
        case (1):
          updateScore (3)
          reactionTimer = 0
          break
        case (2):
          updateScore (2)
          reactionTimer = 0
          break
        case (3):
          updateScore (1)
          reactionTimer = 0
          break
        default:
         reactionTimer = 0
      }
      }

I also tried an if…else statement thinking the switch was the wrong way to go but the result seems the same. The result being inconsistent “bonus” points for being quick at the draw. Idk if this is a flaw with Phaser, where I put my code, or my approach to the solution.
I gotta ask, you don’t have to write the code, but in words, how would you have accounted for a persons reaction time?

For extra clarification, i would test each reaction case by setting the others to updateScore(0). Hypothetically this means the only case that would add points was the one I was trying to troubleshoot. Most the time cases (0) and (1) would fire, but even after the visible timer had passed by two or three seconds (which meant that timeLeft had to have updated so I figured reactionTimer had to have as well.), the score would go up by 3 points. Sometimes cases (2) and (3) didn’t want to fire when two or three seconds had visibly “ticked” off the timer.
This was fun and frustrating and i hope to come back to it when I learn more!

you’re setting reactionTimer to 0 in a whole lot of places yet it only happens once during that function, so maybe put it in one place that always gets executed instead?
the score is something you can write a 1-line formula for, you’re using 4 minus the time spent, with a maximum of 3
the countdown clock does not line up with when moles appear, so you might want to get two timestamps and get the difference between them for a more accurate measure of time

1 Like