Skip to content

Instantly share code, notes, and snippets.

@usmaanalii
Created September 8, 2017 08:01
Show Gist options
  • Save usmaanalii/d53418f23b443f3a757c731cbdab7899 to your computer and use it in GitHub Desktop.
Save usmaanalii/d53418f23b443f3a757c731cbdab7899 to your computer and use it in GitHub Desktop.

30. Whack A Mole

What I learned on this mini-project.

isTrusted

Here, the click event is important in determining the outcome of the game, therefore we need to protect from false clicks that can be be mimicked through cheeky javascript code.

Using the isTrusted property allows you to confirm that the event was generated by a user action.

https://gist.github.com/0936725d9e87676b9a9d6f89c622c692

The code above, will return if the event was not a product of user action (not through a script).

Recursion

This technique was used to prevent the same hole from being used twice in a row. It's important to return the value in the randomHole method, so that it can be used recursively.

https://gist.github.com/8f40b02e6691b8967ed2276d0b0ff46f

Here, the lastHole variable will be assigned to the randomly generated hole. If the following call generates the same hole, the randomHole will be called again until lastHole != hole.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment