我用相同的问题查找其他人,似乎问题是你需要解析为整数。我试过了,但它仍然附加积分而不是将其添加到分数中。我一定是做错了,但我不确定是什么......
这是我的代码片段
var gameButton = function(){
var userVariable = (document.getElementById("userInput").value);
var gameSecret = Math.floor(Math.random() * 100 +1);
var points = userVariable * 3.14;
var score = 0;
//check if valid entry
if(userVariable <2) {
alert("You must choose a number between 2 - 100");
} else {
//check game result
if(gameSecret>userVariable){
document.getElementById('gameSecret').innerHTML ="You won! Game Secret was " +gameSecret;
document.getElementById('points').innerHTML ="You won " +points +" points!";
//I'm pretty certain these next few lines is where I messed up
var points2 = parseInt(points, 10);
var score2 = parseInt(score, 10);
document.getElementById('score').innerHTML += score2+points2;
} else {
答案 0 :(得分:1)
+
运算符是左右关联的,因此连接胜过添加。此外,您必须将字段的现有值作为数字才能添加到其中:
var score = document.getElementById('score').innerHTML;
...
var score2 = parseInt(score, 10);
document.getElementById('score').innerHTML = score2 + points2;