{"id":3255,"date":"2024-11-10T17:40:21","date_gmt":"2024-11-10T09:40:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3255\/"},"modified":"2024-11-10T17:40:21","modified_gmt":"2024-11-10T09:40:21","slug":"js%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99%e7%8c%9c%e6%95%b0%e6%b8%b8%e6%88%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3255\/","title":{"rendered":"js\u5982\u4f55\u7f16\u5199\u731c\u6570\u6e38\u620f"},"content":{"rendered":"<blockquote><p>\n  javascript \u731c\u6570\u6e38\u620f\u7f16\u5199\u6b65\u9aa4\u5982\u4e0b\uff1a\u521b\u5efa\u76ee\u6807\u6570\u5b57\u548c\u7528\u6237\u731c\u6d4b\u7684\u53d8\u91cf\u3002\u751f\u62100\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u76ee\u6807\u6570\u5b57\u3002\u83b7\u53d6\u7528\u6237\u7684\u731c\u6d4b\u3002\u6bd4\u8f83\u731c\u6d4b\u4e0e\u76ee\u6807\u6570\u5b57\uff0c\u63d0\u4f9b\u53cd\u9988\u3002\u91cd\u590d\u6b65\u9aa43-4\uff0c\u76f4\u5230\u731c\u6d4b\u6b63\u786e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110808272784365.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u7f16\u5199\u731c\u6570\u6e38\u620f\u63d2\u56fe\" alt=\"js\u5982\u4f55\u7f16\u5199\u731c\u6570\u6e38\u620f\u63d2\u56fe\" \/><\/p>\n<p><strong>JS \u731c\u6570\u6e38\u620f<\/strong><\/p>\n<p><strong>\u5982\u4f55\u7f16\u5199<\/strong><\/p>\n<p>\u7f16\u5199\u4e00\u4e2a JavaScript \u731c\u6570\u6e38\u620f\u5f88\u7b80\u5355\uff0c\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\u5373\u53ef\uff1a<\/p>\n<ol>\n<li> <strong>\u521b\u5efa\u53d8\u91cf<\/strong>\uff1a\u58f0\u660e\u4e00\u4e2a\u4ee3\u8868\u76ee\u6807\u6570\u5b57\u7684\u53d8\u91cf target \u548c\u4e00\u4e2a\u4ee3\u8868\u7528\u6237\u731c\u6d4b\u7684\u53d8\u91cf guess\u3002<\/li>\n<li> <strong>\u751f\u6210\u76ee\u6807\u6570\u5b57<\/strong>\uff1a\u4f7f\u7528 Math.random() \u51fd\u6570\u751f\u6210\u4e00\u4e2a 0 \u5230 100 \u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5e76\u5c06\u5176\u5206\u914d\u7ed9 target \u53d8\u91cf\u3002<\/li>\n<li> <strong>\u83b7\u53d6\u7528\u6237\u731c\u6d4b<\/strong>\uff1a\u4f7f\u7528 prompt() \u51fd\u6570\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u731c\u6d4b\uff0c\u5e76\u5c06\u7ed3\u679c\u5206\u914d\u7ed9 guess \u53d8\u91cf\u3002<\/li>\n<li> <strong>\u6bd4\u8f83\u731c\u6d4b<\/strong>\uff1a\u5c06 guess \u4e0e target \u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c guess \u7b49\u4e8e target\uff0c\u5219\u8868\u793a\u7528\u6237\u731c\u5bf9\u4e86\u3002\u5982\u679c guess \u5c0f\u4e8e target\uff0c\u5219\u8868\u793a\u731c\u6d4b\u592a\u5c0f\u3002\u5982\u679c guess \u5927\u4e8e target\uff0c\u5219\u8868\u793a\u731c\u6d4b\u592a\u5927\u3002<\/li>\n<li> <strong>\u63d0\u4f9b\u53cd\u9988<\/strong>\uff1a\u6839\u636e\u6bd4\u8f83\u7ed3\u679c\uff0c\u5411\u7528\u6237\u63d0\u4f9b\u53cd\u9988\uff0c\u544a\u8bc9\u4ed6\u4eec\u731c\u6d4b\u662f\u5426\u6b63\u786e\u6216\u9700\u8981\u731c\u6d4b\u66f4\u5c0f\u6216\u66f4\u5927\u3002<\/li>\n<li> <strong>\u91cd\u590d\u6b65\u9aa4 3-5<\/strong>\uff1a\u5faa\u73af\u6267\u884c\u6b65\u9aa4 3-5\uff0c\u76f4\u5230\u7528\u6237\u731c\u5bf9\u4e3a\u6b62\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<p>\u4e0b\u9762\u7684 JavaScript \u4ee3\u7801\u6f14\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u731c\u6570\u6e38\u620f\uff1a<\/p>\n<pre>\/\/ \u751f\u6210\u76ee\u6807\u6570\u5b57\nconst target = Math.floor(Math.random() * 100);\n\nlet guess;\nlet attempts = 0;\n\n\/\/ \u83b7\u53d6\u7528\u6237\u731c\u6d4b\u5e76\u6bd4\u8f83\nwhile (guess !== target) {\n  guess = parseInt(prompt(\"\u8f93\u5165\u4f60\u7684\u731c\u6d4b\uff080-100\uff09\uff1a\"));\n  attempts++;\n\n  if (guess &lt; target) {\n    console.log(\"\u4f60\u7684\u731c\u6d4b\u592a\u5c0f\u4e86\uff01\");\n  } else if (guess &gt; target) {\n    console.log(\"\u4f60\u7684\u731c\u6d4b\u592a\u5927\u4e86\uff01\");\n  }\n}\n\n\/\/ \u731c\u6d4b\u6b63\u786e\nconsole.log(`\u606d\u559c\uff01\u4f60\u731c\u5bf9\u4e86\uff01\u4f60\u7528\u4e86 ${attempts} \u6b21\u731c\u6d4b\u3002`);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5176\u4ed6\u63d0\u793a<\/strong><\/p>\n<ul>\n<li>\u60a8\u53ef\u4ee5\u4f7f\u7528 while \u5faa\u73af\u6216 do-while \u5faa\u73af\u6765\u8ba9\u6e38\u620f\u4e0d\u65ad\u5faa\u73af\uff0c\u76f4\u5230\u7528\u6237\u731c\u5bf9\u4e3a\u6b62\u3002<\/li>\n<li>\u60a8\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u529f\u80fd\uff0c\u4f8b\u5982\u9650\u5236\u7528\u6237\u731c\u6d4b\u7684\u6b21\u6570\u6216\u63d0\u4f9b\u63d0\u793a\u3002<\/li>\n<li>\u4e3a\u4e86\u63d0\u9ad8\u8da3\u5473\u6027\uff0c\u60a8\u53ef\u4ee5\u5c06\u76ee\u6807\u6570\u5b57\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u8303\u56f4\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u6570\u5b57\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u7f16\u5199\u731c\u6570\u6e38\u620f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>javascript \u731c\u6570\u6e38\u620f\u7f16\u5199\u6b65\u9aa4\u5982\u4e0b\uff1a\u521b\u5efa\u76ee\u6807\u6570\u5b57\u548c\u7528\u6237\u731c\u6d4b\u7684\u53d8\u91cf\u3002\u751f\u62100\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u76ee\u6807\u6570\u5b57\u3002\u83b7\u53d6\u7528\u6237\u7684\u731c\u6d4b\u3002\u6bd4\u8f83\u731c\u6d4b\u4e0e\u76ee\u6807\u6570\u5b57\uff0c\u63d0\u4f9b\u53cd\u9988\u3002\u91cd\u590d\u6b65\u9aa43-4\uff0c\u76f4\u5230\u731c\u6d4b\u6b63\u786e\u3002 JS \u731c\u6570\u6e38\u620f \u5982\u4f55\u7f16\u5199 \u7f16\u5199\u4e00\u4e2a JavaScript \u731c\u6570\u6e38\u620f\u5f88\u7b80\u5355\uff0c\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\u5373\u53ef\uff1a \u521b\u5efa\u53d8\u91cf\uff1a\u58f0\u660e\u4e00\u4e2a\u4ee3\u8868\u76ee\u6807\u6570\u5b57\u7684\u53d8\u91cf target \u548c\u4e00\u4e2a\u4ee3\u8868\u7528\u6237\u731c\u6d4b\u7684\u53d8\u91cf guess\u3002 \u751f\u6210\u76ee\u6807\u6570\u5b57\uff1a\u4f7f\u7528 Math.random() \u51fd\u6570\u751f\u6210\u4e00\u4e2a 0 \u5230 100 \u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5e76\u5c06\u5176\u5206\u914d\u7ed9 target \u53d8\u91cf\u3002 \u83b7\u53d6\u7528\u6237\u731c\u6d4b\uff1a\u4f7f\u7528 prompt() \u51fd\u6570\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u731c\u6d4b\uff0c\u5e76\u5c06\u7ed3\u679c\u5206\u914d\u7ed9 guess \u53d8\u91cf\u3002 \u6bd4\u8f83\u731c\u6d4b\uff1a\u5c06 guess \u4e0e target \u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c guess \u7b49\u4e8e target\uff0c\u5219\u8868\u793a\u7528\u6237\u731c\u5bf9\u4e86\u3002\u5982\u679c guess \u5c0f\u4e8e target\uff0c\u5219\u8868\u793a\u731c\u6d4b\u592a\u5c0f\u3002\u5982\u679c guess \u5927\u4e8e target\uff0c\u5219\u8868\u793a\u731c\u6d4b\u592a\u5927\u3002 \u63d0\u4f9b\u53cd\u9988\uff1a\u6839\u636e\u6bd4\u8f83\u7ed3\u679c\uff0c\u5411\u7528\u6237\u63d0\u4f9b\u53cd\u9988\uff0c\u544a\u8bc9\u4ed6\u4eec\u731c\u6d4b\u662f\u5426\u6b63\u786e\u6216\u9700\u8981\u731c\u6d4b\u66f4\u5c0f\u6216\u66f4\u5927\u3002 \u91cd\u590d\u6b65\u9aa4 3-5\uff1a\u5faa\u73af\u6267\u884c\u6b65\u9aa4 3-5\uff0c\u76f4\u5230\u7528\u6237\u731c\u5bf9\u4e3a\u6b62\u3002 \u793a\u4f8b\u4ee3\u7801 \u4e0b\u9762\u7684 JavaScript \u4ee3\u7801\u6f14\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u731c\u6570\u6e38\u620f\uff1a \/\/ \u751f\u6210\u76ee\u6807\u6570\u5b57 const target = Math.floor(Math.random() * [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3255","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3255"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3255\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}