[英]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. 要解决这一点,我需要重新设置变量和数组
guess
和guessed
每个时间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.