[英]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.";
}
}
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个错误:
else if (totalPoints = 100)
while you need ==
else if (totalPoints = 100)
而您需要==
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.