如何计算JS中的百分比差异?

时间:2015-03-07 18:24:19

标签: javascript variables percentage equation rate

我想计算两个值之间的百分比差异。

我正在努力的是变量,我不明白如何使其工作,我甚至不知道如何开始...所以如果我的代码有很多错误我很抱歉。

我希望用户能够比较两个国家/地区的出生率和/或死亡率。应该有第一种选择,用户选择是否想知道出生率或死亡率。然后是选择要比较的第一个国家/地区的选项,以及用户选择第二个国家/地区的第三个选项。 但我真的不知道如何实际做到这一点。到目前为止我得到了这个:

var choice = {
birth: {
    holland: 11,
    brasil: 17,
    irannie, 19,
    //amount of people who are born every 1000 citizens
},
death: {
    holland: 8,
    brasil: 6,
    irannie: 6,

    //amount of people who die every 1000 citizens, note two values are the same.. so what should the result show? I don't understand


}




  document.getElementById('calculate').addEventListener('click', function () {
    var first= +document.getElementById('onecountry').value;
    var second= +document.getElementById('secondcountry').value;
    // var snelheid = +document.getElementById('vervoer').value;





var percentage = first / second * 100;
var resultaat=Math.round(percentage*10)/10

我在这里面临的另一个问题是我不知道如何根据第一个选项显示结果,所以如果用户选择出生率,它应该显示第一胎率为第二胎的百分比率和死亡率相同。

document.getElementById('div2').innerHTML = "Het verschil van het geboortecijfer ten opzichte met het andere land is  <b>" + resultaat + "</b>;

这是在HTML中:

<select id="choice">
    <option value="birth">Birth rate </option>
    <option value="death">Death rate</option>
</select>


<select id="onecountry">
    <option value="holland">Nederland</option>
    <option value="brasil">Brazillie</option>
</select>
<p></p>Het tweede land dat je wil vergelijken
<select id="secondcountry">
    <option value="holland">Nederland</option>
    <option value="brasil">Brazillie</option>
    <option value="irannie">Iran</option>
</select>

<p></p>
<p></p>Het percentageverschil is
<input id="calculate" type="button" value="Bereken!" />

区别是......

2 个答案:

答案 0 :(得分:0)

好吧,我想我知道你正在尝试做什么。由于您的选项值会映射您在出生时使用的按键。死亡,你可以传递形式的值:

var first = choice.birth[document.getElementById('onecountry').value];
var second = choice.death[document.getElementById('secondcountry').value];

你要确保没有任何回复未定义。如果您有这样的对象:

 var myObject = {};

尝试访问未设置的属性:

 myObject.name    // returns undefined
 myObject['name'] // returns undefined

检查它确实存在的关键。

 if (first && second) {
    var percentage = first / second * 100;
    var resultaat=Math.round(percentage*10)/10
 }

希望有所帮助。另外在你的javascript代码中,不要使用html评论,坚持使用javascript代码

 <!-- correct use of comment -->
 <p>Hello</p>
 <script type="text/javascript">
   // correct use of a comment here
   alert("hello");
 </script>

答案 1 :(得分:0)

这应该解决它。我重新编写了计算方法并重新设计了界面。它是英文的,所以你需要翻译它。

保留了您定义的choice对象。那很好。然后,在calculate(),我:

  • 获取用户想要的统计数据(出生或死亡率)
  • 获取第一和第二个国家
  • 访问choice对象(使用choice[statistic]获取内部birthdeath对象,然后获取额外[firstCountry],获取每个国家/地区的值和统计信息}或[secondCountry]获取国家/地区的价值)
  • 计算第一个国家/地区的值作为第二个国家/地区的百分比
  • 显示它。

var choice = {
  birth: {
    holland: 11,
    brasil: 17,
    irannie: 19
  },
  death: {
    holland: 8,
    brasil: 6,
    irannie: 6,
  }
}

function calculate() {
  var statistic = document.getElementById("choice").value;
  var firstCountry = document.getElementById("onecountry").value;
  var secondCountry = document.getElementById("secondcountry").value;
  var firstCountryValue = choice[statistic][firstCountry];
  var secondCountryValue = choice[statistic][secondCountry];
  var percentage = Math.floor((firstCountryValue / secondCountryValue) * 100);
  document.getElementById("result").innerText = "First as a percentage of second: " + percentage;
}
Choose statistic to compare:
<select id="choice">
  <option value="birth">Birth rate</option>
  <option value="death">Death rate</option>
</select>
<br/>Choose first country:
<select id="onecountry">
  <option value="holland">Nederland</option>
  <option value="brasil">Brazillie</option>
</select>
<br/>Choose second country:
<select id="secondcountry">
  <option value="holland">Nederland</option>
  <option value="brasil">Brazillie</option>
  <option value="irannie">Iran</option>
</select>
<br/>

<button onclick="calculate()">Calculate</button>
<span id="result"> 
</span>