简体   繁体   English

为什么 Vanilla JS 不运行我的 if 语句?

[英]Why is Vanilla JS not running my if statement?

So I'm making an Interchange game.所以我正在制作一个互换游戏。

I want the player to enter an answer and my if statements will check if that answer is correct, according to an array that I have set up.我希望玩家输入一个答案,我的 if 语句将根据我设置的数组检查该答案是否正确。 It will randomize so I will use the variable that is randomized to do something like this:它会随机化,所以我将使用随机化的变量来做这样的事情:

function getRandomImage(){
    var num = Math.floor(Math.random() * 10);
    var img = imageArray[num];
    document.getElementById("question-image").innerHTML = ('<img src="' + img + '"width=450px; height=550px; margin-left: auto; margin-right: auto;">');
    console.log("random image!");
    console.log(answers[num]);
    const textarea = document.getElementById("answer-textarea").value;

    if (document.getElementById("answer-textarea").value == answers[num].toString() && skipCounter > 3){
        console.log(answers[num]);
        score++;
        document.getElementById("score-txt").innerHTML = ("<b>Score: </b>" + "<b>" + score + "</b>");
    }

    else if (document.getElementById("answer-textarea").value != answers[num].toString() && skipCounter > 3){
        console.log(answers[num]);
        score--;
        document.getElementById("score-txt").innerHTML = ("<b>Score: </b>" + "<b>" + score + "</b>");
    }
}

HTML: HTML:

<textarea class="answer-textarea" id="answer-textarea"></textarea>

When I run this, AND I enter the correct answer according to my array, it does nothing.当我运行它并根据我的数组输入正确的答案时,它什么也不做。 1 1

Array:大批:

answers = new Array();
answers[0] = "Turbine Interchange";
answers[1] = "Complex Roundabout";
answers[2] = "Cloverleaf Interchange";
answers[3] = "High Five";
answers[4] = "Mixing Bowl Interchange";
answers[5] = "Light Horse Interchange";
answers[6] = "T Interchange";
answers[7] = "Trumpet Interchange";
answers[8] = "Vaanplein Junction";
answers[9] = "Ya'an East Road Interchange";

Is there something wrong with my code?我的代码有问题吗?

EDIT 1: I've updated my code now and I'm checking for my texrarea value, answer, skip counter and current score.编辑 1:我现在更新了我的代码,我正在检查我的 texrarea 值、答案、跳过计数器和当前分数。

console.log(textarea + "\n" + answers[num] + "\n" + skipCounter + "\n" + score);

EDIT 2: Yep the if functions are not even working.编辑 2:是的 if 函数甚至不起作用。 I added a new statement on the bottom of my ifs:我在 ifs 的底部添加了一条新语句:

else {
    console.log("Did nothing!")
}

It all just says "did nothing".这一切都只是说“什么也没做”。

The issue is that you aren't actually adding the answers to the array.问题是您实际上并没有将答案添加到数组中。

You need to use the array push method.您需要使用数组push方法。

answers = new Array();
answers.push("Turbine Interchange");
answers.push("Complex Roundabout");
answers.push("Cloverleaf Interchange");
// etc

Once you make that change it will work as intended.一旦您进行了更改,它将按预期工作。

An alternative method which I would recommend is to initialize the array as it is being created.我推荐的另一种方法是在创建数组时对其进行初始化。

var answers = [
  “Answer one”,
  “Answer two”,
  “Answer three”,
  // etc
];

I've fixed the program.我已经修复了程序。

For future reference and others', here's how I did it.供将来参考和其他人参考,这就是我的做法。

  1. I made the num var global我将num var 设为全局

    var num;变量编号;

    getRandImg(){//bla bla bla} getRandImg(){//bla bla bla}

  2. Separated get randImg() and checkAns()分开 get randImg() 和 checkAns()

  3. Made a Boolean to check if it is the first time laoding, so I dont check for the next answer as @Barmar said.做了一个Boolean来检查是否是第一次laoding,所以我没有像@Barmar所说的那样检查下一个答案。

You seem to be randomizing the question after the user has already answered.在用户已经回答之后,您似乎正在随机化问题。 Is that what it's supposed to do?这是它应该做的吗? – Barmar – 巴尔马尔

Thanks for all of your help stackoverflow people, I've also made a repo for the project.感谢您对 stackoverflow 人员的所有帮助,我还为该项目制作了一个 repo。 if you wanna see the full code.如果你想看完整的代码。 https://github.com/elfryt/whats-the-interchange https://github.com/elfryt/whats-the-interchange

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM