0
我查了一些同樣的問題,似乎問題是你需要解析爲一個整數。我嘗試過,但它仍然附加點而不是將它添加到分數。我必須做的pareseInt錯了,但我不知道是什麼......Javascript,點附加,而不是添加到分數字段?
這裏是我的代碼片段
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 {
我想你只需要設置''通過=''不+ ='innerHTML' 。也應該首先計算結果'(score2 + point2)'。 – 2014-09-20 17:34:24
@KingKing剛剛嘗試過,但現在它不追加或添加。無論點數是多少,分數場均保持不變。 – m1xolyd1an 2014-09-20 17:39:04
如何驗證你真的有一個號碼,然後再添加它:if(Number.isInteger(parseInt(someData)){}; – Vishal 2014-09-20 17:47:04