javascript - switch case js two parameters - Stack Overflow

Please tell me how I can do with two parameters and simplify this code to a normal state. Thank you! Th

Please tell me how I can do with two parameters and simplify this code to a normal state. Thank you! This logic is very necessary for me, I hope for your indulgence.

var treshhold_two = 0;

function Test(attack, ratio) {
  switch (attack) {
    case 0,01:
      switch (ratio) {
        case 2:
          treshhold = 2798,6;
          break;
        case 4:
          treshhold = 3678,16;
          break;
        case 6:
          treshhold = 5757,12;
          break;
      }
      break;
    case >0,01:
      switch (attack, ratio) {
        case 0,03,2:
          treshhold = -5,75712;
          break;
        case 0,03,4:
          treshhold = -5,75712 * 1,1;
          break; // -45%
        case 0,03,6:
          treshhold = -5,75712 * 0,96;
          break; // -52%, and etc.
        ...
      }
      break;
  }
}

Please tell me how I can do with two parameters and simplify this code to a normal state. Thank you! This logic is very necessary for me, I hope for your indulgence.

var treshhold_two = 0;

function Test(attack, ratio) {
  switch (attack) {
    case 0,01:
      switch (ratio) {
        case 2:
          treshhold = 2798,6;
          break;
        case 4:
          treshhold = 3678,16;
          break;
        case 6:
          treshhold = 5757,12;
          break;
      }
      break;
    case >0,01:
      switch (attack, ratio) {
        case 0,03,2:
          treshhold = -5,75712;
          break;
        case 0,03,4:
          treshhold = -5,75712 * 1,1;
          break; // -45%
        case 0,03,6:
          treshhold = -5,75712 * 0,96;
          break; // -52%, and etc.
        ...
      }
      break;
  }
}
Share Improve this question asked Dec 15, 2016 at 10:17 Alexander SKyzZzAlexander SKyzZz 1092 silver badges12 bronze badges 1
  • Why not use if statements? – laurent Commented Dec 15, 2016 at 10:20
Add a ment  | 

3 Answers 3

Reset to default 4

Try:

var treshhold_two = 0;

function Test(attack, ratio) {
  if(attack == 0,01) {
     switch (ratio) {
        case 2:
          treshhold = 2798,6;
          break;
        case 4:
          treshhold = 3678,16;
          break;
        case 6:
          treshhold = 5757,12;
          break;
       }
   }
   else {           
      switch (attack) {
        case 0,03:
              if(ratio==2) treshhold = -5,75712;                 
              if(ratio==4) treshhold = -5,75712 * 1,1;                             
              if(ratio==6) treshhold = -5,75712 * 0,96;                  
              break;
        ...
      }          
  }
}
    //option:1

    var treshhold = 0;

    function Test(attack, ratio) {
      switch (attack) {
        case 0,01:
                if(ratio==2) treshhold = 2798,6;               
          if(ratio==4) treshhold = 3678,16;                          
          if(ratio==6) treshhold = 5757,12;
            break;
        case 0,03:
          if(ratio==2) treshhold = -5,75712;             
          if(ratio==4) treshhold = -5,75712 * 1,1;                         
          if(ratio==6) treshhold = -5,75712 * 0,96;
          break;
          // ...
          }
      }
    }


    //option:2

    var treshhold = 0;

    function Test(attack, ratio) {
      switch (attack) {
        case 0,01:
            switch (ratio) {
            case 2: treshhold = 2798,6; break;
            case 4: treshhold = 3678,16; break;
            case 6: treshhold = 5757,12; break;
          }
        case 0,03:
            switch (ratio) {
            case 2: treshhold = -5,75712;  break;
            case 4: treshhold = -5,75712 * 1,1; break;
            case 6: treshhold = -5,75712 * 0,96;; break;
          }
         // ...
          }
     }

//option:3

var treshhold = 1223456;

function Test(ratio, attack) {
  switch (ratio) {
    case 2:
        switch (attack) {
        case 0,01: ... break;
        case 0,03: ... break;
        case 0,1: ... break;
      }
      break;
    case 4:
        switch (attack) {
        case 0,01: ... break;
        case 0,03: ... break;
        case 0,1: ... break;
      }
      break;
    case 6:
        switch (attack) {
        case 0,01: ... break;
        case 0,03: ... break;
        case 0,1: ... break;
      }
      break;
 }


// option?
var treshhold_two = 0;

function Test(attack, ratio) {
  if (attack == 0.01) {
      switch (ratio) {
        case 2:
          treshhold = 2798.6;
          break;
        case 4:
          treshhold = 3678.16;
          break;
        case 6:
          treshhold = 5757.12;
          break;
      }
    }
    else if (attack > 0.01) {
      switch (attack, ratio) {
        case 0,03,2: // what does it mean ?
          treshhold = -5.75712;
          break;
        case 0,03,4: // what does it mean ?
          treshhold = -5.75712 * 1.1;
          break; // -45%
        case 0,03,6: // what does it mean ?
          treshhold = -5.75712 * 0.96;
          break; // -52%, and etc.
        ...
      }
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744028747a4546167.html

相关推荐

  • javascript - switch case js two parameters - Stack Overflow

    Please tell me how I can do with two parameters and simplify this code to a normal state. Thank you! Th

    9小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信