我的Javascript Math代码有什么问题?

时间:2016-11-28 12:05:38

标签: javascript html

这是一个数字游戏的滑块。如果你的积分超过50,你将得到* 2的金额。滑块允许您选择下注多少。

问题1:但是当用户获胜时,分数不会添加。它显示05,055,0555,而不是5,10,15。 问题2:下注金额也与滑块不对应。它只增加了50或-50。



var amountBet = document.getElementById("myRange").value;
var originalNumber = 0;

function generateNumber() {
  var randomNum = (Math.random() * 100).toFixed(2);
  var pass = 52.5;

  if (randomNum > pass) {

    var el = document.getElementById('number');
    el.textContent = originalNumber += (amountBet);
    msg = 'You won! ';

  } else {

    var el = document.getElementById('number');
    el.textContent = originalNumber -= (amountBet);
    msg = ' You lost! ';

  }

  function myFunction() {
    var x = document.getElementById("myRange").value;
    document.getElementById("demo").innerHTML = "Roll Above " + x + " to win";
  }
}

<div id="demo">Roll Above:</div>

<input type="range" id="myRange" min="0" max="100">
<!-- Slider that should select amount bet -->

<div onclick="myFunction()">Click to see amount</div>
<!-- Click to see amount selected on slider -->

<button onclick="generateNumber()" type="submit" value="Roll" id="button1" class="button button">Roll above 52.99 for *2</button>
<!-- Button to roll -->

<p id="number">0</p>
<!-- Amount of credits user has -->
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

使用Number javascript函数在第一行代码中获取字符串值后,尝试添加此行代码。

var amountBet =document.getElementById("myRange").value;    
var finalBetAmount = Number(amountBet);

答案 1 :(得分:0)

您需要将amountBet移动到该函数中,因为否则您将获得滑块的默认值并获取滑块的数值。

然后将el移到比较之外,因为在两种情况下都是相同的。

稍后可以查看赢家/输家文本。

var amountBet;
var originalNumber = 0;

function generateNumber() {
    var randomNum = Math.random() * 100;
    var pass = 52.5;
    var el = document.getElementById('number');
    amountBet = +document.getElementById("myRange").value;
    if (randomNum > pass) {
        originalNumber += amountBet;
        el.textContent = originalNumber + ' ' + 'You won! ';
    } else {
        originalNumber -= amountBet;
        el.textContent = originalNumber + ' ' + 'You lost! ';
    }
}

function myFunction() {
    var x = document.getElementById("myRange").value;
    document.getElementById("demo").innerHTML = "Roll Above " + x + " to win";
}
<div id="demo">Roll Above:</div>
<input type="range" id="myRange" min="0" max="100">
<div onclick="myFunction()">Click to see amount</div>
<button onclick="generateNumber()" type="submit" value="Roll" id="button1" class="button button">Roll above 52.99 for *2</button>
<p id="number">0</p>

答案 2 :(得分:0)

导致问题:

问题1 =&gt;您正在连接字符串和数字。输入字段的值将采用字符串格式。

问题2 =&gt;每次都会获取滑块的默认值,因为未在函数内部读取输入控件的值。

解决方案:

  • 将范围选择器中的字符串值转换为Number
  • 将字符串值从toFixed(2)转换为Number。 (toFixed函数产生一个字符串输出)。
  • 获取函数内的滑块值。

调试代码:

var amountBet; 
var originalNumber =0;
function generateNumber() {
    amountBet =Number(document.getElementById("myRange").value);
    var randomNum = Number((Math.random()*100).toFixed(2));
    var pass = 52.5;    
    if ( randomNum > pass ) 
    {
          var res = originalNumber+=amountBet;
          var el = document.getElementById('number');
          el.textContent= res;
          msg = 'You won! '; 
    } 
    else 
    { 
        var el = document.getElementById('number');
        var res = originalNumber-=amountBet;
        el.textContent= res;
        msg = ' You lost! ';
    }
}