简体   繁体   English

当我将随机数生成器/猜测器放入函数时,我的JavaScript代码崩溃了。 为什么会崩溃?

[英]My javascript code is crashing when I put a random number generator/guesser into a function. Why does it crash?

I have build a random number generator used to guess numbers. 我建立了一个随机数发生器,用于猜测数字。 I have limited this number guesser not to ask the same number twice and this works outside of the function. 我限制了这个数字猜测者不要问两次相同的数字,这在函数之外有效。 as soon as I put the number generator/guesser into a function the window crashes every few attempts. 一旦将数字生成器/猜测器放入函数中,每几次尝试都会使窗口崩溃。 I believe this has to do with the number generating on an infinite loop. 我相信这与无限循环中生成的数字有关。 Can anyone see what the issue is? 谁能看到问题所在?

Edit: for those who like to see the HTML as well. 编辑:对于那些也喜欢看HTML的人。 I have two simple inputs before the JS: 在JS之前,我有两个简单的输入:

<body>

    <p>Think of a number!</p>

    <select name="fingers" id="mynumber">
        <option>0</option>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
    </select>

    <button id="startguess">Start guessing my number!</button>

Here are the global variables: 以下是全局变量:

        var guessed = [""];

        var guess = "";

Here is the function: 这是函数:

        function doaguess(correctanswer) {

            guess = Math.floor(Math.random() * 6);

            var n = "";

            n = guessed.includes(guess);

            if (n == true) {

                guess = Math.floor(Math.random() * 6);

            } else {

                if (guess == correctanswer) {

                    return (true);

                } else {

                    return (false);

                }

            }

        }

And the rest of the script is this: 脚本的其余部分是这样的:

        document.getElementById("startguess").onclick = function() {

            var mynumber = document.getElementById("mynumber").value;

            var gotit = false;

            var numberofguesses = 1;

            while (gotit == false) {

                if (doaguess(mynumber) == true) {

                    gotit = true;

                    alert("Got it! It was a " + mynumber + ". It took me " + numberofguesses + " guesses.");

                } else {

                    numberofguesses++;
                    guessed.push(guess);

                }

            }

        }

Everything worked fine until I moved the generator into the function doaguess() . 一切正常,直到我将生成器移到函数doaguess() I have tried moving the variables into global from local to change the scope and this made the code work but it now crashes. 我尝试将变量从局部变量移到全局变量以更改范围,这使代码起作用,但现在崩溃了。 Any help is appreciated. 任何帮助表示赞赏。

Edit: for those who like to see the HTML as well. 编辑:对于那些也喜欢看HTML的人。 I have two simple inputs before the JS: 在JS之前,我有两个简单的输入:

If you guess the number after (n == true) you are not returning anything, and the guess could be a repetead one, that should be into a loop also searching for a new guess. 如果您猜出(n == true)之后的数字,那么您什么也不返回,并且该猜想可能是重复的,应该进入循环并寻找新的猜想。 When you guess the number in that case, then the result is not true and the correct answer is added in the guessed array causing the infinite loop. 当您猜测这种情况下的数字时,结果将不正确,并且正确的答案会添加到guessed数组中,从而导致无限循环。

Fix: 固定:

n = guessed.includes(guess);


while (n == true) {

    guess = Math.floor(Math.random() * 6);
    n = guessed.includes(guess);    
}

if (guess == correctanswer) {

       return (true);

   } else {

       return (false);
}

Its this line guessed.push(guess); 其这一行guessed.push(guess); should be guessed.push(mynumber); 应该被guessed.push(mynumber);

Here is it working: 它在这里工作:

 var guessed = [""]; var guess = ""; function doaguess(correctanswer) { guess = Math.floor(Math.random() * 6); var n = guessed.includes(guess); if (n === true) { guess = Math.floor(Math.random() * 6); } else { return guess == correctanswer; } } document.getElementById("startguess").onclick = function() { var mynumber = document.getElementById("mynumber").value; var gotit = false; var numberofguesses = 1; while (!gotit) { if (doaguess(mynumber)) { gotit = true; alert("Got it! It was a " + mynumber + ". It took me " + numberofguesses + " guesses."); } else { numberofguesses++; guessed.push(mynumber); } } } 
 <button id="startguess">Guess</button> <input id="mynumber" type="number"></input> 

I have found the issue. 我发现了问题。 The variables are not resetting every time I try to offer a number so it appears not to work if I try multiple times without reloading the page because the numbers are stored instead of resetting at each new "play". 每次我尝试提供一个数字时变量都不会重置,因此,如果我多次尝试而不重新加载页面,则该变量似乎不起作用,因为存储数字而不是在每个新的“播放”中重置数字。 To resolve this I need to reset the variables and arrays guess and guessed every time the onclick is pressed. 要解决这一点,我需要重新设置变量和数组guessguessed每个时间onclick被按下。 Like this: 像这样:

        document.getElementById("startguess").onclick = function() {

            guessed = [""];

            guess = "";

            var mynumber = document.getElementById("mynumber").value;

            var gotit = false;

            var numberofguesses = 1;

            while (gotit == false) {

                if (doaguess(mynumber) == true) {

                    gotit = true;

                    alert("Got it! It was a " + mynumber + ". It took me " + numberofguesses + " guesses.");

                } else {

                    numberofguesses++;
                    guessed.push(guess);

                }

            }

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我运行此Javascript代码时,为什么浏览器仍然崩溃? - Why does my browser keep crashing when I run this Javascript code? 我的函数在javascript中运行时,我的随机数生成器不断变化 - My random number generator keeps changing when my function runs in javascript 为什么我的代码只能在html中工作,而在将其放入Javascript页面时却不能工作? - Why does my code only works in html but does not work when I put it into a Javascript page? 为什么我的随机数生成器总是给我一个? - Why does my random number generator always give me one? 当我将JavaScript块添加到函数中时,该块不起作用。 有人可以帮忙解释原因吗? - A block of JavaScript does not work when I add it to a function. can anybody help explain why? 当我在Javascript代码中添加特定行时,它会删除HTML函数具有的onclick函数。 - When I add a certain line to my Javascript code it removes the onclick function I have for my HTML function. 这个JavaScript随机数生成器如何工作? - How does this JavaScript random number generator work? 为什么我的JavaScript Prime Number-Generator不起作用? - Why does my JavaScript Prime Number-Generator not work? Javascript-不重复代码的随机报价生成器 - Javascript - Random Quote Generator that does not repeat code 为什么我的 Javascript 计算器 function 在我点击数字按钮时随机返回“undefined”? - Why does my Javascript calculator function return "undefined" RANDOMLY, when I click on number buttons?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM