货币转换器与Js

时间:2016-05-16 17:45:53

标签: javascript html css

所以我想用Js创建一个简单的货币转换器,我想通过创建列表包含5个可用硬币(美元,欧元,GPB,澳元和日元),用户必须从第一个列表中选择他想要的硬币转换,在第二个列表,他必须选择他想要的硬币货币。这是我到目前为止所做的:



function calculate() {
  var amount = parseFloat(document.getElementById("amount").value);
  var select = document.getElementById("select");
  var select1 = document.getElementById("select1");
  var result = document.getElementById("result");

  if (select.value === "USD") && (select1.value === "USD")  {
    result.value = (amount * 1);
  }
  if (select.value === "USD") && (select1.value === "EUR")  {
    result.value = (amount * 0.88);
  }
  if (select.value === "USD") && (select1.value === "AUD")  {
    result.value = (amount * 1.37);
  }
  if (select.value === "USD") && (select1.value === "GBP")  {
    result.value = (amount * 0.69);
  }
  if (select.value === "USD") && (select1.value === "JPY")  {
    result.value = (amount * 108.96);
  }
  if (select.value === "EUR") && (select1.value === "EUR")  {
    result.value = (amount * 1);
  }
  if (select.value === "EUR") && (select1.value === "USD")  {
    result.value = (amount * 1.13);
  }
  if (select.value === "EUR") && (select1.value === "AUD")  {
    result.value = (amount * 1.55);
  }
  if (select.value === "EUR") && (select1.value === "GBP")  {
    result.value = (amount * 0.79);
  }
  if (select.value === "EUR") && (select1.value === "JPY")  {
    result.value = (amount * 123.47);
  }
  if (select.value === "EUR") && (select1.value === "EUR")  {
    result.value = (amount * 1);
  }
  if (select.value === "EUR") && (select1.value === "USD")  {
    result.value = (amount * 1.13);
  }
  if (select.value === "EUR") && (select1.value === "AUD")  {
    result.value = (amount * 1.55);
  }
  if (select.value === "EUR") && (select1.value === "GBP")  {
    result.value = (amount * 0.79);
  }
  if (select.value === "EUR") && (select1.value === "JPY")  {
    result.value = (amount * 123.47);
  }
  if (select.value === "GPB") && (select1.value === "EUR")  {
    result.value = (amount * 1.27);
  }
  if (select.value === "GPB") && (select1.value === "USD")  {
    result.value = (amount * 1.44);
  }
  if (select.value === "GPB") && (select1.value === "AUD")  {
    result.value = (amount * 1.98);
  }
  if (select.value === "GPB") && (select1.value === "GBP")  {
    result.value = (amount * 1);
  }
  if (select.value === "GPB") && (select1.value === "JPY")  {
    result.value = (amount * 157.08);
  }
  if (select.value === "AUD") && (select1.value === "EUR")  {
    result.value = (amount * 0.64);
  }
  if (select.value === "AUD") && (select1.value === "USD")  {
    result.value = (amount * 0.73);
  }
  if (select.value === "AUD") && (select1.value === "AUD")  {
    result.value = (amount * 1);
  }
  if (select.value === "AUD") && (select1.value === "GBP")  {
    result.value = (amount * 0.51);
  }
  if (select.value === "AUD") && (select1.value === "JPY")  {
    result.value = (amount * 79.51);
  }
  if (select.value === "JPY") && (select1.value === "EUR")  {
    result.value = (amount * 0.0081);
  }
  if (select.value === "JPY") && (select1.value === "USD")  {
    result.value = (amount * 0.0092);
  }
  if (select.value === "JPY") && (select1.value === "AUD")  {
    result.value = (amount * 0.013);
  }
  if (select.value === "JPY") && (select1.value === "GBP")  {
    result.value = (amount * 0.0064);
  }
  if (select.value === "JPY") && (select1.value === "JPY")  {
    result.value = (amount * 1);
  }
}

<h1>Convert US Dollars to Euros </h1>
<form>
  <p>
    <label for="amount">Amount:</label>
    <input type="text" id="amount" value="">
    <select id="select">
      <option value="USD">USD </option>
      <option value="EUR">EUR </option>
      <option value="GPB">GBP </option>
      <option value="AUD">AUD </option>
      <option value="JPY">JPY </option>
    </select>
    <select id="select1">
      <option value="USD">USD </option>
      <option value="EUR">EUR </option>
      <option value="GPB">GBP </option>
      <option value="AUD">AUD </option>
      <option value="JPY">JPY </option>
    </select>
    <input type="text" id="result" value="">
  </p>
  <p>
    <input type="button" value="Calculate" onclick="calculate();">
    <input type="reset" value="Reset">
  </p>
</form>
&#13;
&#13;
&#13;

但到目前为止它根本不起作用......我无法理解为什么......我做错了什么?

2 个答案:

答案 0 :(得分:5)

if符号不应在&&个符号之间关闭。所以你在哪里

if (select.value === "EUR") && (select1.value === "EUR")

应该是

if (select.value === "EUR" && select1.value === "EUR")

答案 1 :(得分:2)

问题在于方括号,但您的if语句应该是if else if,因为找到一个匹配项,您不需要检查其他费率。

if (select.value === "USD" && select1.value === "USD")  {
    result.value = (amount * 1);
} else if (select.value === "USD" && select1.value === "EUR")  {
    result.value = (amount * 0.88);
}

更好的方法是创建所有费率的对象,并使用单个if语句检查费率是否存在。

var rates = {
    USD : {
        USD: 1,
        EUR: 0.88,
        AUD: 1.37,
    },
    EUR : {
        EUR : 1,
        USD : 1.13,
        AUD : 1.55,
    }
}

if(rates[select.value] && rates[select.value][select1.value]){
    result.value = amount * rates[select.value][select1.value];
}