ホームページ >ウェブフロントエンド >H5 チュートリアル >JS猜数字游戏如何判断一个数字是否被猜过?
写一个竞猜游戏,用户必须猜一个秘密的数字,在每次猜完后程序会告诉用户他猜的数是太大了还是太小了,直到猜测正确,最后打印出猜测的次数。如果用户连续猜测同一个数字则只算一次。
我就是不知道怎么实现“如果用户连续猜测同一个数字则只算一次。”
没有思路,下面附上我写的代码,就是不知道实现最后一句。
如果有大神看到的话帮忙回答下,真的是想不出来。
<code class="language-text">/**
*
* 用object当做hash来记录猜过的数字
* 假设把所有猜过的数字存在一个array中, 其中有重复的数字
* [1, 2, 5, 6, 3, 5, 1, 6, 2, 6]
*
*/
var guesses = [1, 2, 5, 6, 3, 5, 1, 6, 2, 6];
var guessesHash = {};
var guessesCount = 0; // total guess count exclude duplicated guesses
for (var i = 0; i </code>
用数组或者object把输入过的数存起来即可。<code class="language-js"><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">history</span><span class="o">=</span><span class="p">{},</span>
<span class="nx">secret</span><span class="o">=</span><span class="mi">4</span><span class="p">;</span> <span class="c1">// https://www.xkcd.com/221/</span>
<span class="k">do</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">input</span><span class="o">=</span><span class="nb">parseInt</span><span class="p">(</span><span class="nx">prompt</span><span class="p">(</span><span class="s1">'输入数字: '</span><span class="p">));</span>
<span class="nx">history</span><span class="p">[</span><span class="nx">input</span><span class="p">]</span><span class="o">=</span><span class="kc">true</span><span class="p">;</span>
<span class="k">if</span><span class="p">(</span><span class="nx">input</span><span class="o">></span><span class="nx">secret</span><span class="p">)</span>
<span class="nx">alert</span><span class="p">(</span><span class="s1">'比这个小'</span><span class="p">);</span>
<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="nx">input</span><span class="o"><span class="nx">secret</span><span class="p">)</span>
<span class="nx">alert</span><span class="p">(</span><span class="s1">'比这个大'</span><span class="p">);</span>
<span class="p">}</span> <span class="k">while</span><span class="p">(</span><span class="nx">input</span><span class="o">!==</span><span class="nx">secret</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">count</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">_</span> <span class="k">in</span> <span class="nx">history</span><span class="p">)</span>
<span class="nx">count</span><span class="o">++</span><span class="p">;</span>
<span class="nx">alert</span><span class="p">(</span><span class="s1">'你猜了 '</span><span class="o">+</span><span class="nx">count</span><span class="o">+</span><span class="s1">' 次'</span><span class="p">);</span>
<span class="p">})();</span>
</span></code>
定义个全局变量,记录用户每次输入的值,一样的值不做重复记录,不一样的记录。最后看看这个变量的数。。。。
第一种方法:建个数组,用户输入时检查数组最后一个数是否和输入相等,不相等就push进去。最后输出数组的长度就好了。<code class="language-js"><span class="kd">var</span> <span class="nx">arr</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">];</span>
<span class="kd">function</span> <span class="nx">getLength</span><span class="p">(</span><span class="nx">arr</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">arr</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">v</span><span class="p">,</span><span class="nx">i</span><span class="p">,</span><span class="nx">arr</span><span class="p">){</span>
<span class="k">return</span> <span class="nx">v</span><span class="o">!==</span><span class="nx">arr</span><span class="p">[</span><span class="nx">i</span><span class="o">-</span><span class="mi">1</span><span class="p">];</span>
<span class="p">}).</span><span class="nx">length</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">getLength</span><span class="p">(</span><span class="nx">arr</span><span class="p">));</span> <span class="c1">// 5</span>
</code>
var n=0;