gpt4 book ai didi

Javascript,分数被附加而不是添加到分数字段?

转载 作者:行者123 更新时间:2023-12-03 11:48:22 25 4
gpt4 key购买 nike

我查找了其他有相同问题的人,似乎问题是您需要解析为整数。我尝试过,但它仍然附加分数而不是添加到分数中。我肯定做错了 pareseInt 但我不确定是什么......

这是我的代码片段

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 {

最佳答案

+ 运算符是左右结合的,因此连接胜过加法。另外,您必须以数字形式获取该字段的现有值才能添加到其中:

var score = document.getElementById('score').innerHTML;
...
var score2 = parseInt(score, 10);
document.getElementById('score').innerHTML = score2 + points2;

关于Javascript,分数被附加而不是添加到分数字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25951476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com