2015-06-17 91 views
0

我现在拥有的是一个函数,返回使用随机数生成器放在一起的2个字符串的数组。这些值将帮助组成一个字符串,如“这是”+ v1 +“,这是”+ v2。 v1和v2是动态创建的字符串。我需要根据字符串的值进行操作。我的问题是我不知道如何对v1和v2所代表的所有不同方式进行有效比较。我想我必须做很多if语句,就像我将在代码中显示的方式一样。我知道有一个更好的方法来进行比较。它是什么?测试动态创建的字符串

//the unit function makes sure that a random letter is returned but also makes sure that 
    the first letter returned comes before the second letter in the values array 
function unit(){ 
     var values = ["a", "b", "c", "d", "e" , "f"]; 
     var value1 = Math.floor(Math.random() * (values.length - 1)) + 1; 
     var value2 = Math.floor(Math.random() * value1); 
     if(value1 !== value2){ 
      return [values[value2], values[value1]]; 
     } 

    } 
    //pseusodo-code 
     // i have to do a different operation dependeng on the values of v1 and v2 
     //bad code 
    function conversion(v1, v2, num){ 
     if(v1 == "a" && v2 == "b"){ 
      return 16 * num 
     } 
     if(v1 == "a" && v2 == "c"){ 
      return 32 * num 
     } 
     if(v1 == "a" && v2 == "d"){ 
      return 64 * num 
     } 
     if(v1 == "a" && v2 == "e"){ 
      return 256 * num 
     } 
     if(v1 == "b" && v2 == "c"){ 
      return 18 * num 
     } 
     if(v1 == "b" && v2 == "d"){ 
      return 20 * num 
     } 
     if(v1 == "b" && v2 == "e"){ 
      return 64 * num 
     } 
     if(v1 == "b" && v2 == "f"){ 
      return 256 * num 
     } 
     etc 
    } 
    function string(){ 
     u = unit() 
     v1 = u[0]; 
     v2 = u[1] 
     return "this is " + v1 + " and this " + v2 
    } 
    console.log(string()) 

    // for every 2 a theres 1 b's 
    //for every 16 a there 1 c 
    //for every 32 a there is 1 d 
    //for every 64 a there is 1 e 
    conversion(v1,v2, 2.5) 
    console.log(v1, " ", v2) 

回答

1

难道你不能创建一个查找对象,其中包含所有的乘法因子? 想到这好像它是一个二维表:

var factors = { 
    a: { b: 16, c: 32, d: 64, e: 256 }, 
    b: { c: 18, d: 20, e: 64, f: 256 } 
}; 

然后转换函数变为:

function conversion (v1, v2, num) { 
    if (! factors.hasOwnProperty(v1)) { 
    throw "no conversion defined on first level for value " + v1; 
    } 
    if (! factors[v1].hasOwnProperty(v2)) { 
    throw "no conversion defined on second level for value " + v1 + ", " + v2; 
    } 
    return num * factors[v1][v2]; 
} 

注意,在factors对象而我的手编码上述值,其值也可以以编程方式设置。

+0

我dont't知道如果我能做到这一点.the真正的VAR值= [“汤匙”,“FL液量“,”杯“,”品脱“,”夸脱“,”加仑/秒“]。你的答案会帮助我比较每个元素1汤匙是2盎司我将如何分配这些值....等等..我只是在'a:{b:16,c:32,d:64,e :256}, b:{c:18,d:20,e:64,f:256}'我可以继续c:{b:18,d:20,e:64,f:256}比较每一个。我在检查时根据每个值分配值。我必须考虑这个 –

+0

如果它关于单位之间的转换系数,你可以将'因素'对象看作是定义所有单位之间转换系数的二维矩阵。如果转换系数保持不变,那应该是可行的。 – stj

0

你就可以用这两个值的开关语句作为字符串,像这样:

function conversion(v1,v2,num){ 
    var switchVar = v1 + "," + v2; // or use another separator than comma, depending of your case 
    int m = 0; 
    switch(switchVar){ 
    case "a,b": 
     m = 16; 
     break; 
    case "a,c": 
    ... 
    default: 
     break; 
    } 
    return i*num; 
} 
+0

与您的选择,我将不得不为每种可能的组合写一个案例?如果是这样,我认为这就像写每个组合的陈述语句一样。我想有效地做一些事情。还没有测试过你,但看起来我必须写出所有的可能性。感谢您的答复。 –

+0

@ user2537537是的,你将不得不为你想要的每个值做一个case语句。我以为你在谈论** program **的效率,其中'switch'与ifs语句相比 –