用户在Javascript中输入后如何存储总分?

时间:2017-07-07 16:13:21

标签: javascript jquery

我编写了我的第一个JavaScript来创建自己的冒险游戏。我有一个输入字段供用户输入他的动作,一个按钮用于提交动作,一个输出文本字段用于故事。单击该按钮时,它会启动一个函数来计算用户操作的有效性。

每个动作都有一定的分数(25,50,75)。如果用户在几次尝试后达到100分,他就赢了。如果他达到100以上就输了。只要不满足100分,用户就可以继续尝试不同的动作。

问题是该函数需要将当前总点存储在全局变量中,但它不会这样做。在每个按钮之后单击check功能,检查是否满足100个点并向用户提供提示。但是现在,它只是说"你做到了!"当你达到100分时,这就是它所说的。我做错了什么?

我是JavaScript的初学者,所以我希望我的代码不是一团糟。欢迎任何帮助和建议。 :)

这是我的HTML代码:

<form>
<fieldset>
<label>Enter your answer here:</label>
<input id="userInput" type="text" name="userInput">
<input id="submit" type="button" name="submit" value="Click!" onclick="userAction()">
<textarea id="txtOutput" type="text" readonly="readonly" name="txtOutput">
</textarea>
</fieldset>

我的Javascript代码:

var userInput = document.getElementById("userInput");
var txtOutput = document.getElementById("txtOutput");

txtOutput.value = "(\This is just a test and not the actual game.\) \n\nChoose your method to convince the merchant: \n\nA. compliment him on his work \nB. bribe him with money \nC. initmidate him \nD. blackmail him";

var totalPoints = 0;

function add(points) {
  totalPoints += points;
}

window.userAction = function() {
  var input = userInput.value.toUpperCase();
  var compliment = "A";
  var bribe = "B";
  var intimidate = "C";
  var blackmail = "D";

    if (input == compliment) {
        add(25);
    }
    else if (input == bribe) {
        add(25);
    }
    else if (input == intimidate) {
        add(50);
    }
    else if (input == blackmail) {
        add(75);
    }
    else {
        txtOutput = "Method not found. Choose either A, B, C or D.";
    }
    check();
}

    window.check = function() {
    if (totalPoints <= 25) {
        txtOutput.value = "You have his attention. Keep going.";
        userAction();
    }
    else if (totalPoints <= 50) {
        txtOutput.value = "It's working. Keep going. Don't push it too hard, though.";
        userAction();
    }
    else if (totalPoints <= 75) {
        txtOutput.value = "You almost convinced him. Be careful now!";
        userAction();
    }
    else if (totalPoints = 100) {
        txtOutput.value = "You did it!";
    }
    else if (totalPoints > 100) {
        txtOutput.value = "You pushed it too hard. The merchant runs away!";
    }
    else {
        txtOutput.value = "input not found.";
    }
}

JSfiddle

5 个答案:

答案 0 :(得分:0)

所以你的问题是你正在重新调用userAction()&amp;与你的平等设置vs检查:

// Notice here you check `<=`
 else if (totalPoints <= 75) {
    txtOutput.value = "You almost convinced him. Be careful now!";
}
// Notice here you SET `=`, this should be `===`
// this will ALWAYS be true
else if (totalPoints = 100) {
    txtOutput.value = "You did it!";
}
// Notice here you check `>`
else if (totalPoints > 100) {
    txtOutput.value = "You pushed it too hard. The merchant runs away!";
}

请参阅:https://jsfiddle.net/0mo7peoc/7/

答案 1 :(得分:0)

这是一个解决方案

https://jsfiddle.net/exa2k8vq/1/

您有2个错误:

  1. else if (totalPoints = 100),当您需要==
  2. 你在你的if中调用了userAction(),所以玩家得到了两次

答案 2 :(得分:0)

从window.check

中删除对UserAction的调用

它多次调用同一个动作,因此你可以在第一次点击时完成它

更改此

window.check = function() {
    if (totalPoints <= 25) {
        txtOutput.value = "You have his attention. Keep going.";
        userAction();
    }
    else if (totalPoints <= 50) {
        txtOutput.value = "It's working. Keep going. Don't push it too hard, though.";
        userAction();
    }
    else if (totalPoints <= 75) {
        txtOutput.value = "You almost convinced him. Be careful now!";
        userAction();
    }
    else if (totalPoints == 100) {
        txtOutput.value = "You did it!";
    }
    else if (totalPoints > 100) {
        txtOutput.value = "You pushed it too hard. The merchant runs away!";
    }
    else {
        txtOutput.value = "input not found.";
    }

到这个

    window.check = function() {
    if (totalPoints <= 25) {
        txtOutput.value = "You have his attention. Keep going.";

    }
    else if (totalPoints <= 50) {
        txtOutput.value = "It's working. Keep going. Don't push it too hard, though.";
    }
    else if (totalPoints <= 75) {
        txtOutput.value = "You almost convinced him. Be careful now!";

    }
    else if (totalPoints = 100) {
        txtOutput.value = "You did it!";
    }
    else if (totalPoints > 100) {
        txtOutput.value = "You pushed it too hard. The merchant runs away!";
    }
    else {
        txtOutput.value = "input not found.";
    }
}

这是一个小提琴https://jsfiddle.net/0mo7peoc/15/

也改变了这一行

  

否则if(totalPoints = 100){

到这个

  

否则if(totalPoints == 100){

答案 3 :(得分:0)

只有错误的条件是totalPoints = 100将其更改为totalPoints == 100。但是,您获得此行为的实际原因是您的check方法。在您的每个条件中,您一次又一次地调用userAction方法。这就是你立刻得出结论的原因。

我已经调整了你的小提琴来解决这个问题。请参阅更新的小提琴UPDATED FIDDLE

将您的check方法更新为

window.check = function() {
    if (totalPoints <= 25) {
        txtOutput.value = "You have his attention. Keep going.";
    }
    else if (totalPoints <= 50) {
        txtOutput.value = "It's working. Keep going. Don't push it too hard, though.";
    }
    else if (totalPoints <= 75) {
        txtOutput.value = "You almost convinced him. Be careful now!";
    }
    else if (totalPoints == 100) {
        txtOutput.value = "You did it!";
    }
    else if (totalPoints > 100) {
        txtOutput.value = "You pushed it too hard. The merchant runs away!";
    }
    else {
        txtOutput.value = "input not found.";
    }

注意:我已从此方法中删除了对userAction方法的调用。

答案 4 :(得分:-1)

您只是错误输入了等于运算符==

...
else if (totalPoints == 100) { 
    txtOutput.value = "You did it!"; 
}