{"id":22345534,"url":"https://github.com/gerhynes/whack-a-mole","last_synced_at":"2026-04-27T23:39:59.081Z","repository":{"id":106048165,"uuid":"109870914","full_name":"gerhynes/whack-a-mole","owner":"gerhynes","description":"A whack a mole game built using vanilla JavaScript. Built for Wes Bos' JavaScript 30 course. ","archived":false,"fork":false,"pushed_at":"2018-05-15T22:27:58.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-26T10:14:23.759Z","etag":null,"topics":["javascript","javascript30"],"latest_commit_sha":null,"homepage":"https://gk-hynes.github.io/whack-a-mole/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gerhynes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-07T17:51:43.000Z","updated_at":"2018-05-15T22:27:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"a3790705-ed2a-4b9a-a35d-d34a68609082","html_url":"https://github.com/gerhynes/whack-a-mole","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gerhynes/whack-a-mole","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Fwhack-a-mole","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Fwhack-a-mole/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Fwhack-a-mole/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Fwhack-a-mole/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gerhynes","download_url":"https://codeload.github.com/gerhynes/whack-a-mole/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gerhynes%2Fwhack-a-mole/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32360116,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-27T20:07:02.737Z","status":"ssl_error","status_checked_at":"2026-04-27T20:07:00.910Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","javascript30"],"created_at":"2024-12-04T09:18:01.153Z","updated_at":"2026-04-27T23:39:59.042Z","avatar_url":"https://github.com/gerhynes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Whack-a-mole\r\n\r\nA whack a mole game built solely in vanilla JavaScript for Wes Bos' [JavaScript 30](https://javascript30.com/) course.\r\n\r\n[![Screenshot of the Whack-a-mole game](http://res.cloudinary.com/gerhynes/image/upload/v1519758114/whack-a-mole_vyyfua.jpg)](https://gk-hynes.github.io/whack-a-mole/)\r\n\r\n## Notes\r\n\r\nThe goal is to have moles appear in random holes for random amounts of time. The tally of clicked on moles is also logged.\r\n\r\nFirst, select the holes, moles, and scoreboard.\r\n\r\nCreate a function, `randomTime`, which takes a min and max time, and returns a random time between them.\r\n\r\n```js\r\nfunction randomTime(min, max) {\r\n  return Math.round(Math.random() * (max - min) + min);\r\n}\r\n```\r\n\r\nCreate another function, `randomHole`, which takes in a list of holes and selects one at random.\r\n\r\n`holes` is a NodeList that contains the six holes.\r\n\r\nSet a variable, `hole`, to `holes[idx]`.\r\n\r\nYou will run into a problem in that it could return the same hole twice in a row.\r\n\r\nCreate a let variable, `lastHole`.\r\n\r\nAt the end of the function let `lastHole` = `hole`. This will save a reference to the previous hole and allow you to check.\r\n\r\nIf `hole` equals `lastHole` run the function again.\r\n\r\nIf `hole` doesn't equal `lastHole`, return `hole`. Otherwise return `randomHole(holes)`.\r\n\r\nCreate a function, `peep`, which will cause the moles to pop up.\r\n\r\nSelect a random time (between 200ms and 1s) and a random hole.\r\n\r\nTake the random hole and add a class of `up` to it. This will trigger the CSS to animate it in.\r\n\r\nBut, they will stay up. To remedy this, use `setTimeout` to remove the class after the random time is complete.\r\n\r\n```js\r\nfunction peep() {\r\n  const time = randomTime(200, 1000);\r\n  const hole = randomHole(holes);\r\n  hole.classList.add(\"up\");\r\n  setTimeout(() =\u003e {\r\n    hole.classList.remove(\"up\");\r\n    if (!timeUp) peep();\r\n  }, time);\r\n}\r\n```\r\n\r\nAt this stage `peep()` will run indefinitely.\r\n\r\nCreate a variable, `timeUp`, and set it to false.\r\n\r\nMake a function, `startGame`.\r\n\r\nRestart the scoreboard, set `timeUp` equal to false, and call `peep()`.\r\n\r\nUse a `setTimeout` to set `timeUp` to false after 10 seconds.\r\n\r\nFinally, when you click on a mole, it needs to bonk them on the head.\r\n\r\nMake a function, `bonk`, which takes in an event.\r\n\r\nTake all of the moles and listen for a click on each of them.\r\n\r\nOn all events is a property called `isTrusted`. If you fake clicking something, e.g. using JavaScript, this will be false.\r\n\r\nIf `e` is not `isTrusted` then return.\r\n\r\nCrete a let variable, score, equal to 0.\r\n\r\nInside `bonk()` increment `score`, remove the `up` class, and set the text content of `scoreBoard` to `score`.\r\n\r\n```js\r\nfunction bonk(e) {\r\n  if (!e.isTrusted) return; // Catch cheaters faking clicks\r\n  score++;\r\n  this.classList.remove(\"up\");\r\n  scoreBoard.textContent = score;\r\n}\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgerhynes%2Fwhack-a-mole","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgerhynes%2Fwhack-a-mole","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgerhynes%2Fwhack-a-mole/lists"}