[英]How to take the values from a componentDidUpdate() and save it so that i can use it later for displaying
[英]How do I save the value of a variable on each loop so I can use those values later
我是一个完整的初学者,请原谅我缺乏知识。
我有一个执行多次的代码,变量的值由用户通过提示符给出。 有没有一种方法可以在每个循环上存储该值并在循环结束时显示它。 我应该使用某些元素吗?
谢谢。
var ran = Math.floor(Math.random() * 60) + 1;
var ngs = 9;
var guess = prompt("what's your guess?");
while (ngs > 0) {
if (guess < ran) {
var guess = prompt("nope, that was too low. you have " + ngs + " attempts left. guess again");
ngs = ngs - 1;
} else if (guess > ran) {
var guess = prompt("nope, that was too high. you have " + ngs + " attempts left. guess again.");
ngs = ngs - 1;
} else if (guess == ran) {
alert("gj. you win. your guesses were: ");
}
}
alert("no more attempts. you lose. your guesses were: ");
最后,我需要警报以显示用户的猜测。
epascarello在上面的评论中是正确的。 我个人建议将其写入字符串,但是将其压入数组也可以。
var ran = Math.floor(Math.random() * 60) + 1;
var ngs = 9;
var guess = prompt("what's your guess?");
var guesses = ""
while (ngs > 0) {
if (guess < ran) {
var guess = prompt("nope, that was too low. you have " + ngs + " attempts left. guess again");
ngs = ngs - 1;
guesses += guess + ' ';
} else if (guess > ran) {
var guess = prompt("nope, that was too high. you have " + ngs + " attempts left. guess again.");
ngs = ngs - 1;
guesses += guess + ' ';
} else if (guess == ran) {
alert("gj. you win. your guesses were: " + guesses);
}
}
alert("no more attempts. you lose. your guesses were: " + guesses);
var ran = Math.floor(Math.random() * 60) + 1;
var ngs = 9;
var guess = prompt("what's your guess?");
var guesses = []
while (ngs > 0) {
if (guess < ran) {
var guess = prompt("nope, that was too low. you have " + ngs + " attempts left. guess again");
ngs = ngs - 1;
guesses.push(guess);
} else if (guess > ran) {
var guess = prompt("nope, that was too high. you have " + ngs + " attempts left. guess again.");
ngs = ngs - 1;
guesses.push(guess);
} else if (guess == ran) {
alert("gj. you win. your guesses were: " + guesses.join(' '));
}
}
alert("no more attempts. you lose. your guesses were: " + guesses.join(' '));
在if / else中跳过对guess变量的重新声明。 其次,您可以查看以下链接,以了解Array类。 Javascript数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.