如何计算变量的百分比

时间:2018-02-21 09:31:33

标签: javascript

每次点击按钮时,我都试图让变量上升一定百分比。我理解如何做到这一点,我只需要知道如何将变量乘以百分比。

有人可以向我解释如何将变量乘以百分比吗?

function gainCoinsPS6MP() {
  if (coins >= Cost6MP) {
    coinsPS += 1;
    coins -= Cost6MP;
    (((Cost6MP = Cost6MP += 10 % of Cost6MP)))
  } else {
    alert("You trying to scam me!?");
  }

三个括号中的内容是我需要用代码替换的内容,以便每当您使用其中一个时将对象的价格更改为百分之十。

1 个答案:

答案 0 :(得分:0)

你需要JavaScript:

首先创建你的变量:

<script type="text/javascript">
var x = 0;
</script>

然后您可以通过按钮点击增加您的变量:

<button onclick="x = x + ((x * 30)/100)">Click to Increment</button>

30是您想要增加的百分比x

因评论编辑:

我建议写一个函数:

<script>
function myFunction() {
    var x = 10;
    var y = 20;
    if (x > y) {
        x = x + ((x * 30)/100)
    } else {
      document.write("x is not bigger than y")
    }
}
</script>

然后只需在按钮点击事件中调用该函数:

<button onclick="myFunction()">Check if x bigger than y</button>