gpt4 book ai didi

Javascript向计数器加一,然后在特定事件发生后显示

转载 作者:行者123 更新时间:2023-12-03 02:19:32 25 4
gpt4 key购买 nike

我有一种基于猜谜游戏的 JS 和平,其中计算机选择随机数字,用户尝试猜测该数字。该程序会给用户提示并显示用户的最佳猜测。我想做的是:每次输出这 3 个 if 语句之一时,我希望它为计数器加一。当输出最后一句时,我希望程序显示进行了多少次猜测。

    if(guess < to guess){
document.getElementById('hint').innerHTML = "The number you are trying to
guess is higher";
document.getElementById('lower').innerHTML = "Best lower guess at the moment " +
guess;

}else if(guess > to guess){
document.getElementById('hint').innerHTML = "The number you are trying to
guess is lower";
document.getElementById('higher').innerHTML = "Best higher guess at the
moment " + guess;

}else if(guess == to guess){
document.getElementById('hint').innerHTML = "congratulations! You guessed the right number " + "
right number was " + to guess + "<br>"+ "How many guesses it took ";
document.getElementById('lower').innerHTML = "";
document.getElementById('higher').innerHTML = "";

最佳答案

增加数字非常简单,只需存储一个名为numberOfGuesses的变量即可:

var numberOfGuesses = 0

您可以将其加 1,如下所示:

猜测数 += 1

您可以使用此功能将猜测次数不断增加 1,直到您准备好完成并显示该数字。

关于Javascript向计数器加一,然后在特定事件发生后显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49222028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com