testing dynamically created strings

47 views Asked by At

what i have now is a function that return an array of 2 string that were put together using a random number generator. those values will help make up a string like "this is" + v1 + " and this is" + v2. v1 and v2 are the dynamically created strings. I need to do operations depending on the values of the string. my problem is that im not sure on how to do an efficient comparison of all the different ways the v1 and v2 represent. I think I would have to do a lot of if statements like the way i will show in the code. I know there is a better way to do the comparisons. What is it?

  //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)
2

There are 2 answers

2
stj On BEST ANSWER

Can't you create a lookup object which holds all the multiplication factors? Think of this as if it were a two-dimensional table:

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

Then the conversion function becomes:

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];
}

Note that while I hand-coded the values in the factors object above, its values can also be set programatically.

2
AdminXVII On

You could do a switch statement with the two values as a string, like this:

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;
}