我的計算器的非腳本部分是(與所有的輸入):我想在HTML Javascript中製作一個分數計算器,我需要簡化一小部分,我該怎麼做?
<body>
<p class="box">Fraction Calculator</p>
<input type="number" id="n1">
<select id="operation">
<option value="1">X</option>
<option value="2">/</option>
<option value="3">+</option>
<option value="4">-</option>
</select>
<input type="number" id="n2">
<br>
<input type="number" id="n3">
<input type="number" id="n4" style="margin-left:37px">
<br>
<button style="margin-left:130px"; onClick="document.getElementById('fraction').innerHTML = option()">Go!</button>
<p id="fraction"></p>
</body>
我的選項功能是這樣的:
function option()
{
selection = document.getElementById('operation').value;
if(selection == "1"){
a = multiply();
return a
}
else if(selection == "2"){
b = division();
return b
}
else if(selection == "3"){
c = addition();
return c
}
else{
d = subtraction();
return d
}
}
所以我現在對我的乘法部分FO我的計算器代碼是這樣的:
function multiply()
{
Number1 = parseInt(document.getElementById("n1").value);
Number2 = parseInt(document.getElementById("n2").value);
Number3 = parseInt(document.getElementById("n3").value);
Number4 = parseInt(document.getElementById("n4").value);
var topm = Number1 * Number2;
var botm = Number3 * Number4;
return topm + "/" + botm; }
所以目前我的計算器工作,但沒有一個分數被簡化。我將如何能夠簡化這些分數? (我很體面的新代碼)謝謝:) 這裏是的jsfiddle鏈接:https://jsfiddle.net/Ethang70/npgL3gd9/
我真的希望你縮進代碼,並將其上傳的Git過程中丟失,還包括的jsfiddle –
好它並複製奇怪,反正的jsfiddle鏈接已經被添加或在這裏(https://jsfiddle.net/Ethang70/npgL3gd9 /) – Ethang70
也許只是搜索分數簡化算法... http://www.jstor.org/stable/41187600?seq=1#page_scan_ tab_contents – Aeldred