简体   繁体   English

我试图在 if 语句中更改文本的值,但它不起作用我认为我使用了错误的逻辑

[英]im trying to change the value of a text inside an if statement but it doesnt work i think i use wrong logic

So i have made this code that when i click the check button it changes the text of message every time , depending on the number input in guess variable .所以我制作了这段代码,当我单击检查按钮时,它每次都会更改消息文本,具体取决于猜测变量中输入的数字。

Though my logic to declare the variables in the beggining and then use them seems okey to me, it doesnt work Can someone explain why my logic is wrong?虽然我在开始时声明变量然后使用它们的逻辑对我来说似乎没问题,但它不起作用有人可以解释为什么我的逻辑是错误的吗?

// for example i declared 
let message = document.querySelector('.message').textContent;

//and then i used message inside if to change the text
message = '‼ Type a Number';

why is this wrong?为什么这是错的?

document.querySelector('.check').addEventListener('click', function () {
      let score = 20;
      const guess = Number(document.querySelector('.guess').value);
      let message = document.querySelector('.message').textContent;
      if (!guess) {
        message = '‼ Type a Number';
      } else if (secretNumber === guess) {
        message = 'You Won 🥳';
      } else if (secretNumber < guess) {
        message = 'It is a lower Number!';
        score += -1;
        document.querySelector('.score').textContent = score;
      } else if (secretNumber > guess) {
        message = 'It is a higher Number!';
        score += -1;
        document.querySelector('.score').textContent = score;
      }
    });

Your problems (or the two that jump out at me) are that you redefine score every time the function is run.您的问题(或两个跳出来的问题)是每次运行函数时都重新定义score That is, every time you click the button, score is reset to 20 and can only take on values of 20 or 19 depending on the user's input.也就是说,每次单击按钮时, score都会重置为 20,并且只能取2019的值,具体取决于用户的输入。 The other problem is you never re-assign the textContent of the .message element.另一个问题是您永远不会重新分配.message元素的textContent

To resolve this, just move that declaration of score outside of the function and add an extra message reassignment line like so:要解决这个问题,只需将score声明移到函数之外并添加额外的消息重新分配行,如下所示:

let score = 20;
document.queyrSelector(".check").addEventListener("click", function() {
    // the 'let score' line has been removed from in here
    const guess = Number(document.querySelector('.guess').value);
    let message = document.querySelector('.message').textContent;
    // Rest of your code ...
    document.querySelector(".message").textContent = message;
});

Also note (as Bravo pointed out ) that your use of += and - overlaps - you should just use the compound assignment operator with subtraction (ie, score -= 1 not score += -1 . This also works for other operators, such as multiplication *= and division /= .)另请注意(正如Bravo 指出的那样),您使用+=-重叠 - 您应该只使用带有减法的复合赋值运算符(即score -= 1而不是score += -1 。这也适用于其他运算符,例如作为乘法*=和除法/= 。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在制作一个Clicker游戏,我试图使用js更改文本 <p> 标签 - I'm making a clicker game and im trying to use the js to change the text in a <p> tag 我试图为学校开一张彩票,但是当我不得不画数字时,它不起作用(第59行) - Im trying to make a lottoticket for school, but when i have to draw the numbers it doesnt work(line 59) 当我在这里 onclick 并存储在数组中时,试图获取按钮的值? 知道我做错了什么吗? - Trying to get the value of the buttons when i onclick here and store in an array? any idea what im doing wrong? 尝试在我认为是数组的地方使用join()会引发无方法错误 - Trying to use join() on what I think is an array is throwing an no method error 尝试使用pouchDB,说我认为它没有定义 - Trying to use pouchDB, saying that it's not defined when i think it is 正则表达式在我使用$时不起作用 - Regex doesnt work when I use $ 我可以使用 setAttribute 来更改文本节点内的文本吗? - Can I use setAttribute to change the text inside the text node? 我是 discord 机器人场景的新手,每次我输入终端“node index.js”时它都不起作用 - Im new at the discord bot scene and everytime i type in the terminal “node index.js” it doesnt work Angular LightBox不起作用。 我做错了什么? - Angular LightBox doesnt work. What do I do wrong? 我正在尝试使用 axios 表单数据将 js 数组发送到 php 但它一直失败 - im trying to send a js array to php using axios formdata but it keep on failing i get [objext object] at console and if im trying to use stringify ""
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM