简体   繁体   English

用户使用Javascript输入后如何存储总积分?

[英]How do I store total points after user input in Javascript?

I'm coding my first JavaScript create your own adventure game. 我正在编写我的第一个JavaScript代码,以创建自己的冒险游戏。 I have an input field for the user to enter his actions, a button to submit the action and an output text field for the story. 我有一个供用户输入其动作的输入字段,一个用于提交动作的按钮和一个故事的输出文本字段。 When the button is clicked it starts a function to calculate the effectiveness of the user action. 单击该按钮后,它将启动一个函数来计算用户操作的有效性。

Each action has a certain amount of points (25, 50, 75). 每个动作都有一定数量的点(25、50、75)。 If the user reaches 100 points after a few tries, he wins. 如果用户在几次尝试后达到100分,他将获胜。 If he reaches over 100 he loses. 如果他超过100,他就会输。 As long as 100 points aren't met, the user may keep on trying different actions. 只要不达到100分,用户就可以继续尝试其他操作。

The problem is that the function needs to store the current total points in a global variable, but it doesn't do that. 问题在于该函数需要将当前总点数存储在全局变量中,但是并不能这样做。 After every button click the check function checks if the 100 points are met and gives hints to the user. 在每个按钮上单击后, check功能将检查是否达到100分,并向用户提示。 But right now, it just says "You did it!" 但是现在,它只是说“您做到了!” which is what it says when you meet 100 total points. 当您达到100总分时,这就是它的意思。 What am I doing wrong? 我究竟做错了什么?

I'm a total beginner at JavaScript, so I hope my code isn't a complete mess. 我是JavaScript的初学者,所以我希望我的代码不是一团糟。 Any help and advice is welcome. 欢迎任何帮助和建议。 :) :)

Here is my HTML code: 这是我的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>

And my Javascript code: 和我的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.";
    }
}

The JSfiddle JSfiddle

So your problem is you are re-invoking userAction() & with your equality setting vs checking: 因此,您的问题是您要重新调用userAction()并使用相等性设置与检查:

// 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!";
}

See: https://jsfiddle.net/0mo7peoc/7/ 参见: https : //jsfiddle.net/0mo7peoc/7/

Here is a solution 这是一个解决方案

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

You had 2 errors: 您有2个错误:

  1. else if (totalPoints = 100) while you need == else if (totalPoints = 100)而您需要==
  2. you called userAction() in your if, so player got point twice 您在if中调用了userAction(),因此玩家获得了两次积分

Remove your calls to UserAction from within window.check 从window.check中删除对UserAction的调用

it is calling the same action multiple times and hence you get you did it on first click 它多次调用同一动作,因此您在第一次单击时就完成了操作

change this 改变这个

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.";
    }

to this 对此

    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.";
    }
}

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

also change this line 也改变这条线

else if (totalPoints = 100) { 否则,如果(totalPoints = 100){

to this 对此

else if (totalPoints == 100) { 否则,如果(totalPoints == 100){

Only wrong condition is totalPoints = 100 change it to totalPoints == 100 . 唯一错误的条件是totalPoints = 100将其更改为totalPoints == 100 But the actual reason you are getting this behavior is your check method. 但是,您得到此行为的实际原因是您的check方法。 In each of your condition, you are calling userAction method again and again. 在每种情况下,您一次又一次地调用userAction方法。 This is why you reach to the conclusion at once. 这就是为什么您立即得出结论的原因。

I have made the adjustment to your fiddle to fix this issue. 我已对您的提琴进行了调整,以解决此问题。 See updated fiddle UPDATED FIDDLE 查看更新的小提琴UPDATED FIDDLE

Update your check method to 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.";
    }

Note: I have removed calls to your userAction method from this method. 注意:我已经从该方法中删除了对您的userAction方法的调用。

You simply mistyped equals operator, == . 您只是错误地输入了等于运算符==

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

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

相关问题 如何将来自 vue.js 的用户输入存储到 javascript 上的数组中? - How do I store user input from vue.js into an array on javascript? 如何获取用户的输入并将其存储以在 Javascript function 中使用? - How do I take a user's input and store it to be used in a Javascript function? 通过vanilla javascript提交表单后,如何删除用户在输入标签中输入的输入? - How do I remove the input typed in by user in an input tag after form submission through vanilla javascript? 使用 JavaScript,如何在页面重新加载后保持用户输入? - Using JavaScript , how do I keep user input on page after it has reloaded? 如何在HTML和Javascript中将输入存储到变量中? - How do I store an input into a variable in HTML and Javascript? 如果用户使用 JavaScript 单击 cookie 上的按钮,我该如何存储? - How do I store if a user clicked a button on a cookie using JavaScript? 如何使用高效的 Javascript 在本地存储多个用户更改? - How do I store multiple user changes locally with efficient Javascript? 如何在 JavaScript 中使用 prompt() 存储用户输入? - How to store user input using prompt() in JavaScript? 如何将用户输入存储到数组中? - How do you store a user input into an array? 我如何获得 JavaScript 中的准确总数 - How do i get the accurate total for in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM