单击按钮递减数字直至达到 0

发布于 2025-01-15 22:17:13 字数 1156 浏览 0 评论 0原文

我正在做一个项目,有点卡住了。我确信我错过了一些东西,但又不知道我到底错过了什么。

我提取了我正在做的事情的一般概念并将其放入 JS 小提琴中。

所以基本上,我想在每次单击按钮时(使用 vanilla JS)将显示的数字(200)减少指定数字范围之间的随机生成的数字。让它发挥一点作用。单击该按钮时,它会递减。

不过,我想继续递减它,直到显示的数字为 0。然而,我遇到的问题是,每次单击按钮时,显示的数字都会更新,但仍然从 200 中减去,而不是从更新的值中减去。

例如,我可以单击一次,数字将从 200 跳到 189。当我再次单击时,数字实际上会跳到 195,这意味着它是从 200 中减去,而不是从 189 中减去新生成的金额。Jsfiddle

https://jsfiddle.net/miguerurso/0wmtgd67/5/

html:

<head></head>
<body>
<div id="number" style="font-size: 2em;"></div>
<button>decrement</button>
</body>

JS:

const numberCont = document.getElementById('number');
const button = document.querySelector('button');

let x = 200;

numberCont.innerHTML = x;

button.addEventListener("click", function(){
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min)
      }
      const randomNumResult = randomNum(7, 12)

     let updatedX = x - randomNumResult;

      numberCont.innerHTML = updatedX;
})

I'm working on a project and am a bit stuck. I am sure I am missing something but have not what exactly I'm missing.

I extracted the general concept of what I'm doing and threw it onto a JS fiddle.

So basically, I am wanting to decrement the displayed number (200) by a randomly generated number between a specified number range every time the button is clicked (using vanilla JS). Got it to work a bit. When the button is clicked, it does decrement.

However I am wanting to continue to decrement it until the displayed number is 0. The issue I'm running into however is, every time the button is clicked the displayed number updates but still subtracting from 200, not from the updated value.

For example, I can click once and the number will jump from 200 down to 189. When I click again, the number will actually jump up to say 195, meaning it's subtracting from 200 rather than subtracting the new generated amount from 189.

Jsfiddle: https://jsfiddle.net/miguerurso/0wmtgd67/5/

html:

<head></head>
<body>
<div id="number" style="font-size: 2em;"></div>
<button>decrement</button>
</body>

JS:

const numberCont = document.getElementById('number');
const button = document.querySelector('button');

let x = 200;

numberCont.innerHTML = x;

button.addEventListener("click", function(){
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min)
      }
      const randomNumResult = randomNum(7, 12)

     let updatedX = x - randomNumResult;

      numberCont.innerHTML = updatedX;
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海蓝天 2025-01-22 22:17:13

您还需要将 updateX 分配给 x。目前您仅将其赋予元素。试试这个。

const numberCont = document.getElementById('number');
const button = document.querySelector('button');

let x = 200;

numberCont.innerHTML = x;

button.addEventListener("click", function(){
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min)
      }
      const randomNumResult = randomNum(7, 12)

      x = Math.floor(0, x - randomNumResult);

      numberCont.innerHTML = x;
}

You need assign the updateX To x as well. Currently you are only giving it to the element. Try this.

const numberCont = document.getElementById('number');
const button = document.querySelector('button');

let x = 200;

numberCont.innerHTML = x;

button.addEventListener("click", function(){
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min)
      }
      const randomNumResult = randomNum(7, 12)

      x = Math.floor(0, x - randomNumResult);

      numberCont.innerHTML = x;
}
故事↓在人 2025-01-22 22:17:13

您实际上并未将 x 的值更改为新的减去值。您必须这样做才能继续从变量中减去。我重写了您的事件侦听器以更改 x 的值而不低于 0。

button.addEventListener("click", function(){
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min)
    }

    x = Math.max(x - randomNum(7, 12), 0);

    numberCont.innerHTML = x;
})

You're not actually changing the value of x to the new subtracted value. You'll have to do this to keep subtracting from the variable. I rewrote your event listener to change the value of x without going below 0.

button.addEventListener("click", function(){
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min)
    }

    x = Math.max(x - randomNum(7, 12), 0);

    numberCont.innerHTML = x;
})
回忆躺在深渊里 2025-01-22 22:17:13

只需更改一行代码:

let updatedX = x - randomNumResult;
numberCont.innerHTML = updatedX;

即可:

x = x - randomNumResult;
numberCont.innerHTML = x;

Just change one line of code:

let updatedX = x - randomNumResult;
numberCont.innerHTML = updatedX;

with:

x = x - randomNumResult;
numberCont.innerHTML = x;
楠木可依 2025-01-22 22:17:13

x 中减去它,而不是将其放入新变量中。
https://jsfiddle.net/sean7777/fvgchjb3/1/

Subtract it from x instead of putting it into a new variable.
https://jsfiddle.net/sean7777/fvgchjb3/1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文