![](/img/trans.png)
[英]What is going on in this code ? I do not understand how this works could someone please step me through this
[英]Could someone please tell me how I could simplify this code?
在實踐中,我有這段代碼,如果有辦法讓它變得更容易,做所有這些似乎很累? 但它工作正常嗎? 因為這段代碼不僅有 *(乘法)它還有 3 /(除法)。 如果它只有乘法,它很容易做到,但它也有那些除法,所以這樣做變得更加困難。 如果有人可以幫助我,謝謝。
function calculateLength() {
var lengthinput = parseFloat(document.getElementById('lengthinput').value);
var oper = document.getElementById('lengthselector1').value;
var oper2 = document.getElementById('lengthselector2').value;
if(oper === 'k' && oper2 === 'm')
{
document.getElementById('lengthresult').value = lengthinput*1000 || 0;
}
if(oper === 'k' && oper2 === 'd')
{
document.getElementById('lengthresult').value = lengthinput*10000 || 0;
}
if(oper === 'k' && oper2 === 'c')
{
document.getElementById('lengthresult').value = lengthinput*100000 || 0;
}
if(oper === 'k' && oper2 === 'mi')
{
document.getElementById('lengthresult').value = lengthinput*1000000 || 0;
}
if(oper === 'k' && oper2 === 'mic')
{
document.getElementById('lengthresult').value = lengthinput*1.0000E+9 || 0;
}
if(oper === 'k' && oper2 === 'de')
{
document.getElementById('lengthresult').value = lengthinput*100 || 0;
}
if(oper === 'k' && oper2 === 'h')
{
document.getElementById('lengthresult').value = lengthinput*10 || 0;
}
if(oper === 'k' && oper2 === 'me')
{
document.getElementById('lengthresult').value = lengthinput/1000 || 0;
}
if(oper === 'k' && oper2 === 'g')
{
document.getElementById('lengthresult').value = lengthinput/1000000 || 0;
}
if(oper === 'k' && oper2 === 'z')
{
document.getElementById('lengthresult').value = lengthinput/1.0000E+18 || 0;
}
if(oper === 'k' && oper2 === 'i')
{
document.getElementById('lengthresult').value = lengthinput*39370.0787 || 0;
}
if(oper === 'k' && oper2 === 'a')
{
document.getElementById('lengthresult').value = lengthinput*1.0000E+13 || 0;
}
}
您的所有if
條件都測試oper==='k'
,因此它可以成為圍繞所有 rest 的一個if
語句。 為oper2
創建一個查找表:
function calculateLength() {
var lengthinput = parseFloat(document.getElementById('lengthinput').value);
var oper = document.getElementById('lengthselector1').value;
if (oper === 'k') {
var oper2 = document.getElementById('lengthselector2').value;
var coefficient = {
'i': 39370.0787,
'z': 1e-18,
'g': 1e-6,
'me': 1e-3,
'h': 10,
'de': 100,
'm': 1e3,
'd': 1e4,
'c': 1e5,
'mi': 1e6,
'mic': 1e9,
'a': 1e13
}[oper2]; // <-- here we perform the lookup
if (coefficient) {
document.getElementById('lengthresult').value = lengthinput*coefficient || 0;
}
}
}
請注意,代碼中的1.0000E+9
等於1e9
。 額外的十進制零不會改變任何東西,指數部分的+
也不會改變。
在某些情況下,您的代碼使用標准表示法,例如 1000000,也可以用科學計數法編寫:1e6。 我會嘗試對 10 的所有冪使用相同的符號,這樣您就可以更清楚地看到不同系數的比較。
此外,除以10 的冪與乘以10 的冪相同,其中指數為負:例如:
length/1e18 === length*1e-18
這意味着您可以將所有這些系數寫為乘數和科學記數法(不規則的 39370.0787 除外,您可以選擇保留標准記數法)。
只是為了比較,您可以編寫沒有科學記數法的查找表(“映射對象”),如下所示:
var coefficient = {
'i': 39370.0787,
'z': 0.000000000000000001,
'g': 0.000001,
'me': 0.001,
'h': 10,
'de': 100,
'm': 1000,
'd': 10000,
'c': 100000,
'mi': 1000000,
'mic': 1000000000,
'a': 10000000000000
}[oper2]; // <-- here we perform the lookup
您可以使用查找表來查找系數,例如
const coefficient = { k: { mic: 1.0000E+9, i: 39370.0787 } };
並將其與
document.getElementById('lengthresult').value = lengthinput*coefficient[oper][oper2] || 0;
例子:
function calculateLength() {
const coefficient = { k: { mic: 1.0000E+9, i: 39370.0787 } };
const lengthinput = parseFloat(document.getElementById('lengthinput').value);
const oper = document.getElementById('lengthselector1').value;
const oper2 = document.getElementById('lengthselector2').value;
if (oper in coefficient && oper2 in coefficient[oper]) {
document.getElementById('lengthresult').value = lengthinput*coefficient[oper][oper2] || 0;
}
}
這樣您就可以輕松擴展它:
function calculateLength() {
const coefficient = {
k: { mic: 1.0000E+9, i: 39370.0787 },
l: { abc: 123, def: 567 }
};
const lengthinput = parseFloat(document.getElementById('lengthinput').value);
const oper = document.getElementById('lengthselector1').value;
const oper2 = document.getElementById('lengthselector2').value;
if (oper in coefficient && oper2 in coefficient[oper]) {
document.getElementById('lengthresult').value = lengthinput*coefficient[oper][oper2] || 0;
}
}
你可以試試這個:
function calculateLength() {
const data = [
{
oper1: "k",
oper2: "m",
time: 1000,
},
{
oper1: "k",
oper2: "d",
time: 10000,
},
];
//---
var lengthinput = parseFloat(document.getElementById("lengthinput").value);
//---
var oper = document.getElementById("lengthselector1").value;
var oper2 = document.getElementById("lengthselector2").value;
//---
for (var count = 0; count < data.length; count++) {
if (data[count].oper1 === oper && data[count].oper2 === oper2) {
document.getElementById("lengthresult").value =
lengthinput * data[count].time || 0;
}
}
}
只是另一個輕微的變化。 我在看到其他答案之前寫了這個,所以我想我不妨把它包括在內......
function calculateLength() {
var lengthinput = parseFloat(document.getElementById('lengthinput').value);
var oper = document.getElementById('lengthselector1').value;
var oper2 = document.getElementById('lengthselector2').value;
if (oper === 'k') {
document.getElementById('lengthresult').value = (function () {
switch (oper2) {
case 'm':
return lengthinput * 1000 || 0;
case 'd':
return lengthinput * 10000 || 0;
case 'c':
return lengthinput * 100000 || 0;
case 'mi':
return lengthinput * 1000000 || 0;
case 'mic':
return lengthinput * 1.0000E+9 || 0;
case 'de':
return lengthinput * 100 || 0;
case 'h':
return lengthinput * 10 || 0;
case 'me':
return lengthinput / 1000 || 0;
case 'g':
return lengthinput / 1000000 || 0;
case 'z':
return lengthinput / 1.0000E+18 || 0;
case 'i':
return lengthinput * 39370.0787 || 0;
case 'a':
return lengthinput * 1.0000E+13 || 0;
}
})();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.