{"id":1191,"date":"2023-09-11T22:28:50","date_gmt":"2023-09-11T13:28:50","guid":{"rendered":"https:\/\/tippang.net\/?p=1191"},"modified":"2023-09-11T22:28:52","modified_gmt":"2023-09-11T13:28:52","slug":"a-simple-javascript-game-number-guesser","status":"publish","type":"post","link":"https:\/\/tippang.net\/?p=1191","title":{"rendered":"A simple JavaScript game &#8220;Number Guesser&#8221;"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2.png\" alt=\"\" class=\"wp-image-1192\" style=\"width:840px;height:auto\" srcset=\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2.png 1024w, https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2-300x200.png 300w, https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2-768x512.png 768w, https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2-600x400.png 600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>I&#8217;m going to provide you with a simple JavaScript game called &#8220;Number Guesser&#8221;. In this game, the player tries to guess a random number between 1 and 100. The script will provide hints (too high or too low) until the player guesses the correct number. The game can be played in the browser console.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Here&#8217;s the code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javascriptCopy code<code>\/\/ Number Guesser Game\n\n\/\/ Game variables\nlet randomNumber = Math.floor(Math.random() * 100) + 1;\nlet attempts = 0;\n\n\/\/ Function to process user's guess\nfunction guessNumber() {\n  let userGuess = prompt(\"Guess a number between 1 and 100: \");\n  attempts++;\n\n  if (userGuess &lt; 1 || userGuess &gt; 100) {\n    alert(\"Invalid input. Please enter a number between 1 and 100.\");\n    return guessNumber();\n  }\n\n  if (userGuess == randomNumber) {\n    alert(`Congratulations! You guessed the correct number (${randomNumber}) in ${attempts} attempts.`);\n  } else if (userGuess &lt; randomNumber) {\n    alert(\"Too low. Try again!\");\n    return guessNumber();\n  } else {\n    alert(\"Too high. Try again!\");\n    return guessNumber();\n  }\n}\n\n\/\/ Start the game\nconsole.log(\"Welcome to the Number Guesser game! Guess a number between 1 and 100.\");\nguessNumber();\n<\/code><\/pre>\n\n\n\n<p>To play the game:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open a browser (preferably Google Chrome).<\/li>\n\n\n\n<li>Press <code>Ctrl + Shift + J<\/code> (Windows\/Linux) or <code>Cmd + Option + J<\/code> (Mac) to open the Developer Console.<\/li>\n\n\n\n<li>Copy and paste the code into the console and press <code>Enter<\/code>.<\/li>\n\n\n\n<li>Follow the prompts to play the game. The console will provide feedback on whether your guesses are too high, too low, or correct.<\/li>\n<\/ol>\n\n\n\n<p>The game ends when the player guesses the correct number.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m going to provide you with a simple JavaScript game called &#8220;Number Guesser&#8221;. In this<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"footnotes":""},"categories":[246],"tags":[],"class_list":["post-1191","post","type-post","status-publish","format-standard","hentry","category-game"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A simple JavaScript game &quot;Number Guesser&quot; - tippang.net<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tippang.net\/?p=1191\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:locale\" content=\"en_US\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:type\" content=\"article\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:title\" content=\"A simple JavaScript game &quot;Number Guesser&quot; - tippang.net\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:description\" content=\"I&#8217;m going to provide you with a simple JavaScript game called &#8220;Number Guesser&#8221;. In this\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tippang.net\/?p=1191\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:site_name\" content=\"tippang.net\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-11T13:28:50+00:00\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-11T13:28:52+00:00\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:image\" content=\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2.png\" class=\"yoast-seo-meta-tag\" \/>\n<meta name=\"author\" content=\"charles kim\" class=\"yoast-seo-meta-tag\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" class=\"yoast-seo-meta-tag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" class=\"yoast-seo-meta-tag\" \/>\n\t<meta name=\"twitter:data1\" content=\"charles kim\" class=\"yoast-seo-meta-tag\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" class=\"yoast-seo-meta-tag\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" class=\"yoast-seo-meta-tag\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/tippang.net\/?p=1191#article\",\"isPartOf\":{\"@id\":\"https:\/\/tippang.net\/?p=1191\"},\"author\":{\"name\":\"charles kim\",\"@id\":\"https:\/\/tippang.net\/#\/schema\/person\/5fba0966333bf1aa9f72ad464d264d4a\"},\"headline\":\"A simple JavaScript game &#8220;Number Guesser&#8221;\",\"datePublished\":\"2023-09-11T13:28:50+00:00\",\"dateModified\":\"2023-09-11T13:28:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/tippang.net\/?p=1191\"},\"wordCount\":124,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/tippang.net\/#organization\"},\"image\":{\"@id\":\"https:\/\/tippang.net\/?p=1191#primaryimage\"},\"thumbnailUrl\":\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2.png\",\"articleSection\":[\"Game\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/tippang.net\/?p=1191#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tippang.net\/?p=1191\",\"url\":\"https:\/\/tippang.net\/?p=1191\",\"name\":\"A simple JavaScript game \\\"Number Guesser\\\" - tippang.net\",\"isPartOf\":{\"@id\":\"https:\/\/tippang.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/tippang.net\/?p=1191#primaryimage\"},\"image\":{\"@id\":\"https:\/\/tippang.net\/?p=1191#primaryimage\"},\"thumbnailUrl\":\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2.png\",\"datePublished\":\"2023-09-11T13:28:50+00:00\",\"dateModified\":\"2023-09-11T13:28:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/tippang.net\/?p=1191#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tippang.net\/?p=1191\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tippang.net\/?p=1191#primaryimage\",\"url\":\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2.png\",\"contentUrl\":\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2.png\",\"width\":1024,\"height\":683},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tippang.net\/?p=1191#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tippang.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A simple JavaScript game &#8220;Number Guesser&#8221;\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tippang.net\/#website\",\"url\":\"https:\/\/tippang.net\/\",\"name\":\"tippang.net\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/tippang.net\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tippang.net\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/tippang.net\/#organization\",\"name\":\"tippang.net\",\"url\":\"https:\/\/tippang.net\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tippang.net\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/tippang.net\/wp-content\/uploads\/2021\/02\/cropped-\u110e\u1162\u1102\u1165\u11af\u110b\u1161\u110b\u1175\u110f\u1169\u11ab1_\u1100\u1165\u1107\u116e\u11a8\u110b\u1175.png\",\"contentUrl\":\"https:\/\/tippang.net\/wp-content\/uploads\/2021\/02\/cropped-\u110e\u1162\u1102\u1165\u11af\u110b\u1161\u110b\u1175\u110f\u1169\u11ab1_\u1100\u1165\u1107\u116e\u11a8\u110b\u1175.png\",\"width\":280,\"height\":280,\"caption\":\"tippang.net\"},\"image\":{\"@id\":\"https:\/\/tippang.net\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/tippang.net\/#\/schema\/person\/5fba0966333bf1aa9f72ad464d264d4a\",\"name\":\"charles kim\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tippang.net\/#\/schema\/person\/image\/\",\"url\":\"\/\/www.gravatar.com\/avatar\/fdb7dc5bc7fe5f3bf11d8491a1e8d9c4?s=96&#038;r=g&#038;d=wavatar\",\"contentUrl\":\"\/\/www.gravatar.com\/avatar\/fdb7dc5bc7fe5f3bf11d8491a1e8d9c4?s=96&#038;r=g&#038;d=wavatar\",\"caption\":\"charles kim\"},\"description\":\"Hello, Nice to meet you!\",\"url\":\"https:\/\/tippang.net\/author\/charles-kim\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A simple JavaScript game \"Number Guesser\" - tippang.net","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tippang.net\/?p=1191","og_locale":"en_US","og_type":"article","og_title":"A simple JavaScript game \"Number Guesser\" - tippang.net","og_description":"I&#8217;m going to provide you with a simple JavaScript game called &#8220;Number Guesser&#8221;. In this","og_url":"https:\/\/tippang.net\/?p=1191","og_site_name":"tippang.net","article_published_time":"2023-09-11T13:28:50+00:00","article_modified_time":"2023-09-11T13:28:52+00:00","og_image":[{"url":"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2.png","type":"","width":"","height":""}],"author":"charles kim","twitter_card":"summary_large_image","twitter_misc":{"Written by":"charles kim","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/tippang.net\/?p=1191#article","isPartOf":{"@id":"https:\/\/tippang.net\/?p=1191"},"author":{"name":"charles kim","@id":"https:\/\/tippang.net\/#\/schema\/person\/5fba0966333bf1aa9f72ad464d264d4a"},"headline":"A simple JavaScript game &#8220;Number Guesser&#8221;","datePublished":"2023-09-11T13:28:50+00:00","dateModified":"2023-09-11T13:28:52+00:00","mainEntityOfPage":{"@id":"https:\/\/tippang.net\/?p=1191"},"wordCount":124,"commentCount":0,"publisher":{"@id":"https:\/\/tippang.net\/#organization"},"image":{"@id":"https:\/\/tippang.net\/?p=1191#primaryimage"},"thumbnailUrl":"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2.png","articleSection":["Game"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/tippang.net\/?p=1191#respond"]}]},{"@type":"WebPage","@id":"https:\/\/tippang.net\/?p=1191","url":"https:\/\/tippang.net\/?p=1191","name":"A simple JavaScript game \"Number Guesser\" - tippang.net","isPartOf":{"@id":"https:\/\/tippang.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/tippang.net\/?p=1191#primaryimage"},"image":{"@id":"https:\/\/tippang.net\/?p=1191#primaryimage"},"thumbnailUrl":"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2.png","datePublished":"2023-09-11T13:28:50+00:00","dateModified":"2023-09-11T13:28:52+00:00","breadcrumb":{"@id":"https:\/\/tippang.net\/?p=1191#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tippang.net\/?p=1191"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tippang.net\/?p=1191#primaryimage","url":"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2.png","contentUrl":"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-2.png","width":1024,"height":683},{"@type":"BreadcrumbList","@id":"https:\/\/tippang.net\/?p=1191#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tippang.net\/"},{"@type":"ListItem","position":2,"name":"A simple JavaScript game &#8220;Number Guesser&#8221;"}]},{"@type":"WebSite","@id":"https:\/\/tippang.net\/#website","url":"https:\/\/tippang.net\/","name":"tippang.net","description":"","publisher":{"@id":"https:\/\/tippang.net\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tippang.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/tippang.net\/#organization","name":"tippang.net","url":"https:\/\/tippang.net\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tippang.net\/#\/schema\/logo\/image\/","url":"https:\/\/tippang.net\/wp-content\/uploads\/2021\/02\/cropped-\u110e\u1162\u1102\u1165\u11af\u110b\u1161\u110b\u1175\u110f\u1169\u11ab1_\u1100\u1165\u1107\u116e\u11a8\u110b\u1175.png","contentUrl":"https:\/\/tippang.net\/wp-content\/uploads\/2021\/02\/cropped-\u110e\u1162\u1102\u1165\u11af\u110b\u1161\u110b\u1175\u110f\u1169\u11ab1_\u1100\u1165\u1107\u116e\u11a8\u110b\u1175.png","width":280,"height":280,"caption":"tippang.net"},"image":{"@id":"https:\/\/tippang.net\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/tippang.net\/#\/schema\/person\/5fba0966333bf1aa9f72ad464d264d4a","name":"charles kim","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tippang.net\/#\/schema\/person\/image\/","url":"\/\/www.gravatar.com\/avatar\/fdb7dc5bc7fe5f3bf11d8491a1e8d9c4?s=96&#038;r=g&#038;d=wavatar","contentUrl":"\/\/www.gravatar.com\/avatar\/fdb7dc5bc7fe5f3bf11d8491a1e8d9c4?s=96&#038;r=g&#038;d=wavatar","caption":"charles kim"},"description":"Hello, Nice to meet you!","url":"https:\/\/tippang.net\/author\/charles-kim"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/tippang.net\/index.php?rest_route=\/wp\/v2\/posts\/1191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tippang.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tippang.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tippang.net\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/tippang.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1191"}],"version-history":[{"count":1,"href":"https:\/\/tippang.net\/index.php?rest_route=\/wp\/v2\/posts\/1191\/revisions"}],"predecessor-version":[{"id":1193,"href":"https:\/\/tippang.net\/index.php?rest_route=\/wp\/v2\/posts\/1191\/revisions\/1193"}],"wp:attachment":[{"href":"https:\/\/tippang.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tippang.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tippang.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}