如何处理超出范围的值?切换或if语句?

时间:2016-10-26 06:55:09

标签: javascript switch-statement

我有一组测试分数,其值介于10到990之间。我想将每个分数放在特定范围内。

这是我用过的脚本:

function between(x, min, max) {
  return (x >= min && x <= max);
}

var overallScore = 960;

if(between(overallScore, 905, 990))  {
  var output = "905 - 990";
}
if(between(overallScore, 785, 900))  {
  var output = "785 - 900";
}
if(between(overallScore, 605, 780))  {
  var output = "605 - 780";
}
if(between(overallScore, 405, 600))  {
  var output = "405 - 600";
}
if(between(overallScore, 255, 400))  {
  var output = "255 - 400";
}
if(between(overallScore, 10, 250))  {
  var output = "10 - 250";
}

alert(output);

如果totalScore值在10到990范围内,则此工作正常,超出此范围的值将返回范围为“10 - 250”。处理这个问题的最佳方法是什么?我想过使用switch语句,但我很难看到如何做到这一点。我非常欢迎任何评论或帮助。

3 个答案:

答案 0 :(得分:1)

我实际上会使用包含您的范围的对象,然后创建一个简单的函数来检查它们。这样就更容易维护,更容易理解。

var ranges = [
  { min: 10, max: 250},
  { min: 255, max: 400},
  { min: 405, max: 600},
  { min: 605, max: 780},
  { min: 785, max: 900},
  { min: 905, max: 990}
];

function getScoreRange(score){
  var output = 'out of range'; //set to either empty or null or default value for out of range scores.
  ranges.forEach(function(range){
    if(score >= range.min && score <= range.max){
      output = range.min + ' - ' + range.max;
    }
  });
  return output;
};
alert(getScoreRange(960));

现在,您可以轻松更改逻辑,添加新范围或更改它们而不会进行太多更改,您也可以将评分方法与其他范围重用,也可以用于其他目的。

使用JSFiddle:https://jsfiddle.net/workingClassHacker/c06pq2w0/3/

答案 1 :(得分:0)

您只需在var output

之前声明if-blocks即可

function between(x, min, max) {
  return (x >= min && x <= max);
}
var overallScore = 6000;
var output = ""
if (between(overallScore, 905, 990)) {
  output = "905 - 990";
}
if (between(overallScore, 785, 900)) {
  output = "785 - 900";
}
if (between(overallScore, 605, 780)) {
  output = "605 - 780";
}
if (between(overallScore, 405, 600)) {
  output = "405 - 600";
}
if (between(overallScore, 255, 400)) {
  output = "255 - 400";
}
if (between(overallScore, 10, 250)) {
  output = "10 - 250";
}
console.log(output);

在方法执行开始之前发生变量提升时,你的上一个var被执行了。

答案 2 :(得分:0)

你说过901,782等等的价值不可能发生;这意味着我们可以忽略它们并执行通常的操作,即最后使用if / else if和最终else

function check(overallScore) {
  var output;
  if (overallScore > 990) {
    output = "too high"; //...whatever you want for too high...
  } else if (overallScore > 900) {
    output = "905 - 990";
  } else if (overallScore > 780) {
    output = "785 - 900";
  } else if (overallScore > 600) {
    output = "605 - 780";
  } else if (overallScore > 400) {
    output = "405 - 600";
  } else if (overallScore > 250) {
    output = "255 - 400";
  } else if (overallScore > 9) {
    output = "10 - 250";
  } else {
    output = "too low"; //...whatever you want for too low...
  }
  console.log(overallScore + ": " + output);
}
check(1000);
check(5);
check(200);

无需between功能。

如果范围确实存在差距,那么您仍然使用if / else if但添加下限(可能使用between函数):

function between(x, min, max) {
  return (x >= min && x <= max);
}

function check(overallScore) {
  var output;
  if (overallScore > 990) {
    output = "too high"; //...whatever you want for too high
  } else if (between(overallScore, 905, 990)) {
    output = "905 - 990";
  } else if (between(overallScore, 785, 900)) {
    output = "785 - 900";
  } else if (between(overallScore, 605, 780)) {
    output = "605 - 780";
  } else if (between(overallScore, 405, 600)) {
    output = "405 - 600";
  } else if (between(overallScore, 255, 400)) {
    output = "255 - 400";
  } else if (between(overallScore, 10, 250)) {
    output = "10 - 250";
  } else {
    output = "too low"; //...whatever you want for too low
  }
  console.log(overallScore + ": " + output);
}
check(1000);
check(5);
check(200);