[英]How can I reset a for statement on every input with Javascript?
I am trying to make a simple game in Javascript and I need some help because this language is new to me.我正在尝试用 Javascript 制作一个简单的游戏,我需要一些帮助,因为这种语言对我来说是新的。 In this game a random number from 1 to 100 appears.在这个游戏中会出现一个从 1 到 100 的随机数。 Then the user writes the number he sees and clics on the submit guess button.然后用户写下他看到的数字并点击提交猜测按钮。 After that, a new random number appears and the user has to input the previous number and the new one and so on until he inputs an incorrect number.之后,出现一个新的随机数,用户必须输入以前的数字和新的数字,依此类推,直到他输入错误的数字。
Here is how I start the game这是我开始游戏的方式
function createGame() {
let x = Math.floor(Math.random() * 100 + 1);
document.getElementById("valop1").innerHTML = x;
numbers.push(x);
}
And this is my main algorithm.这是我的主要算法。 SubmitGuessButton makes reference to the button the user clics when he wants to enter a new number, like the one he sees on the screen and the previous ones. SubmitGuessButton 会引用用户在想要输入新号码时点击的按钮,就像他在屏幕上看到的和之前看到的一样。
document.getElementById("submitGuessButton").onclick = function() {
let i;
const miResultado = guessField.value;
for (i = 0; i < numbers.length; i++) {
if (guessField.value == numbers[i]) {
finalScore++;
} else {
console.log("inccorrecto!!");
}
}
createGame();
};
Of course this works fine but it does not make what I want.当然,这很好用,但它并没有达到我想要的效果。 I tried everything to make it works but it never behaves like I want.我想尽一切办法让它工作,但它从来没有像我想要的那样表现。 I tried restarting the for loop but it does not work.我尝试重新启动 for 循环,但它不起作用。 Any suggestions?有什么建议?
welcome to the world of JavaScript!欢迎来到 JavaScript 的世界!
If you are having trouble with syntax i would highly recommend writing your game in "psuedo code" first.如果您在语法方面遇到问题,我强烈建议您先用“伪代码”编写游戏。 Make sure each line does exactly what you want it to do, and try to keep things as simple as possible.确保每一行都完全符合您的要求,并尽量保持简单。
Also please update your question to include the full code for your game.另外请更新您的问题以包含您游戏的完整代码。
you declare miResultado
but you don't reference it again你声明了miResultado
但你不再引用它
try writing a separate function that checks the value of each value in the numbers array one at a time.尝试编写一个单独的函数,一次检查一个 numbers 数组中每个值的值。 HINT: you are going to want to compare 2 arrays (the numbers array and the array the user inputs that are supposed to match the number array)提示:您将要比较 2 个数组(数字数组和应该与数字数组匹配的用户输入数组)
This demo will randomly get a number 1 to 100, the number is revealed for testing, look in CSS and uncomment the style when done testing.该演示将随机获得一个数字 1 到 100,该数字用于测试,测试完成后查看 CSS 并取消对样式的注释。 User will guess and will get feedback on how far or close on each guess.用户会猜测,并会得到关于每次猜测多远或接近多远的反馈。 When the user succeeds in guessing the correct number, they are notified along with how many guesses it took.当用户成功猜出正确的数字时,会通知他们猜了多少次。
The reset function is:复位函数为:
game.onreset = init;
triggered by clicking:通过点击触发:
`<input type='reset'>`
within a <form>
.在<form>
。
In order to understand how it works read the following:为了了解它是如何工作的,请阅读以下内容:
var game = document.forms[0]; var counter = 0; window.onload = init; game.onreset = init; game.onsubmit = takeAGuess; function init(e) { var game = document.forms[0]; var ui = game.elements; var mystery = getRandom(1, 100); ui.test.value = mystery; ui.number.value = ''; ui.message.value = 'Take a guess'; ui.button.style.display = 'inline-block'; return guessingGame(null); } function takeAGuess(e) { e.preventDefault(); var number = this.number.value; var end = guessingGame(number); if (end) { this.button.style.display = 'none'; } } function guessingGame(digit) { var msg = game.message; var N = parseInt(digit, 10); var X = parseInt(test.value); counter++; var rng = Math.abs(X - N) if (N === X) { msg.textContent = `You won with ${counter} guesses!`; return true; } if (rng <= 10) { msg.textContent = `You are very close`; return false; } if ((rng > 10) && (rng <= 20)) { msg.textContent = `You are getting there`; return false; } if ((rng > 20) && (rng <= 40)) { msg.textContent = `You are far from close`; return false; } else if (rng > 40) { msg.textContent = `You are way too far`; return false; } else { return false; } } function getRandom(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; }
fieldset { width: fit-content; } /* Uncomment when done testing #test { display: none }*/
<form id='game'> <fieldset> <legend>Guess a Number between 1 to 100</legend> <output id='message'></output> <br><output id='test'></output><br> <input id='number' type='number' min='1' max='100'> <input id='button' type='submit'> <input type='reset'> </fieldset> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.