简体   繁体   English

将If-Else语句转换为Switch语句

[英]Convert If-Else statement to Switch Statement

I recently completed a problem in CodeWars using an if-else statement, but I wanted to retry it and use a switch statement instead. 我最近使用if-else语句完成了CodeWars中的一个问题,但我想重试它,而改用switch语句。 Too bad that it is not working the way that I thought it would! 太糟糕了,它无法按我认为的方式工作!

The problem I am solving is to take in a distance(s) that an Ironman triathlon athlete has completed and return an object that shows a key based on whether the athlete should be Swimming, Biking or Running with a value of the length of the race to go. 我要解决的问题是,在铁人三项铁人三项运动员已经完成的距离内,并根据该运动员是游泳,骑自行车还是跑步,并返回与比赛时间有关的值,该对象显示出一个关键去。

My If-Else Solution: 我的If-Else解决方案:

function iTri(s) {  
  var triLength = 140.60;  
  var result = {};  
  var str = ' to go!';  
  var lengthLeft = (triLength - s).toFixed(2);  

  if (s === 0) {
    return 'Starting Line... Good Luck!';
  } else if (s <= 2.4) {
    result.Swim = lengthLeft + str;
  } else if (s <= 114.4) {
    result.Bike = lengthLeft + str;
  } else if (s < 130.60) {
    result.Run = lengthLeft + str;
  } else if (s < 140.60) {
    result.Run = 'Nearly there!';
  } else {
    return 'You\'re done! Stop running!';
  }
  return result;
  }

The (non-working) Switch statement: (无效)Switch语句:

function iTri(s){
  let tri = (2.4 + 112 + 26.2).toFixed(2);
  let left = tri - s;
  let str = ' to go!'
  let result = {};

  switch(s) {
    case (s === 0):
      return "Starting Line... Good Luck!";
      break;
    case (s <= 2.4):
      result.Swim = left + str;
      return result;
      break;
    case (s <= 114.4):
      result.Bike = left + str;
      return result;
      break;
    case (s <= 130.60):
      result.Run = left + str;
      return result;
      break;
    case (s < 140.60):
      result.Run = 'Nearly there!';
      return result;
      break;
    default:
      return 'You\'re done! Stop running!';
  }
}

These are the tests: 这些是测试:

Test.describe("Example tests",_=>{
Test.assertSimilar(iTri(36),{'Bike':'104.60 to go!'});
Test.assertSimilar(iTri(103.5),{'Bike':'37.10 to go!'});
Test.assertSimilar(iTri(2),{'Swim':'138.60 to go!'});
});

And the Output: 和输出:

✘ Expected: '{ Bike: \'104.60 to go!\' }', instead got: '\'You\\\'re done! Stop running!\''
✘ Expected: '{ Bike: \'37.10 to go!\' }', instead got: '\'You\\\'re done! Stop running!\''
✘ Expected: '{ Swim: \'138.60 to go!\' }', instead got: '\'You\\\'re done! Stop running!\''  

Also is it worth it to convert it to a switch statement? 将它转换为switch语句是否值得? What are benefits/drawbacks of doing it as if/else vs switch? 好像/否则vs切换一样,这样做的好处/缺点是什么?

Is it even worth it to try to convert it to a switch statement? 尝试将其转换为switch语句是否值得?

No. switch is only useful if you have multiple exact matches. 仅当您有多个完全匹配时,编号switch才有用。 This is not the case for you. 事实并非如此。

If you turn the problem around slightly then you can use a switch like this. 如果您稍微解决问题,则可以使用类似的开关 Your switch is very close to this already 您的开关已经很接近了

 function iTri(s) { var triLength = 140.60; var result = {}; var str = ' to go!'; var lengthLeft = (triLength - s).toFixed(2); switch(true) { case s === 0: return 'Starting Line... Good Luck!'; case s <= 2.4: result.Swim = lengthLeft + str; break; case s <= 114.4: result.Bike = lengthLeft + str; break; case s < 130.60: result.Run = lengthLeft + str; break; case s < 140.60: result.Run = 'Nearly there!'; break; default: return 'You\\'re done! Stop running!'; } return result; } console.log(iTri(0)); console.log(iTri(2)); console.log(iTri(50)); console.log(iTri(120)); console.log(iTri(135)); console.log(iTri(145)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM