无法在此JS函数中找到错误

时间:2016-03-27 04:37:04

标签: javascript

我尝试创建一个函数,当输入在给定的一组范围内时,该函数返回某个字符串。使用案例:我想根据给定城市的温度更改网页的颜色。我有一个函数,它接受一个数字并返回一个颜色变体。然而,似乎总是回归"粉红色"。有什么帮助吗?

~/.ssh/config

如果在没有所有这些条件的情况下有更简单的方法来执行此功能,请告诉我。我知道输入正确传递,但不能在出错的逻辑中弄清楚。

谢谢!

5 个答案:

答案 0 :(得分:4)

如果继续执行,ifelse if表达式都没有colorTemperatureResult = 'whatever'来阻止它们被评估为不必要。

将字符串分配从return 'whatever'更改为>,然后就可以了。

但是,您的代码不必要地复杂,您也可以完全消除function colorTemperature(temperature) { if(temperature <= -10) { return 'midnightblue'; } else if(temperature <= 10) { return 'darkblue'; } else if(temperature <= 20) { return 'royalblue'; } else if(temperature <= 30) { return 'steelblue'; } else if(temperature <= 100) { // snip } else if(temperature <= 110) { return 'gold'; } else { return 'orangered' } } 比较:

common: &default_settings

  class_transformer:
    # Disable all Akka instrumentations
    com.newrelic.instrumentation.akka-2.0:
      enabled: false
    com.newrelic.instrumentation.akka-2.1:
      enabled: false
    com.newrelic.instrumentation.akka-2.2:
      enabled: false

    # Disable all Netty instrumentations
    com.newrelic.instrumentation.netty-3.4:
      enabled: false
    com.newrelic.instrumentation.netty-3.8:
      enabled: false
    com.newrelic.instrumentation.netty-4.0.0:
      enabled: false
    com.newrelic.instrumentation.netty-4.0.8:
      enabled: false

    # Disable all Play 2 instrumentations
    com.newrelic.instrumentation.play-2.1:
      enabled: false
    com.newrelic.instrumentation.play-2.2:
      enabled: false
    com.newrelic.instrumentation.play-2.3:
      enabled: false
    # New in Release 3.22, the Play 2.4 instrumentation does not respect
    # the older play2_instrumentation configuration setting 
    com.newrelic.instrumentation.play-2.4:
      enabled: false

    # Disable all Scala-language instrumentations
    com.newrelic.instrumentation.scala-2.9.3:
      enabled: false

答案 1 :(得分:1)

else指的是最后一个if声明。只需将除{1}之外的所有if语句切换为else if语句。

答案 2 :(得分:1)

我为您提供了2行解决方案。看看:

function colorTemperature (temperature) {
  var strings = ['midnightblue', 'darkblue', 'royalblue', 'steelblue', 'deepskyblue', 'lightblue', 'lightyellow', 'lemonchiffron', 'khaki', 'orange', 'gold', 'orangered', 'pink']
  return strings[Math.floor(temperature / 10) + 1] || 'whatever'
}

答案 3 :(得分:0)

首先,您创建一个全局colorTemperatureResult 您应该在函数范围中创建一个:

function colorTemperature(temperature){
    var colorTemperatureResult = "";
    ...

一种简单的方法是改变:

 else {
        colorTemperatureResult = 'pink';
      }

检查以前是否分配了一个:

colorTemperatureResult = colorTemperatureResult ? colorTemperatureResult : 'pink';

您还可以编写一个简单的函数:

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

并使用它:

if(between(temperature,0,10)) {colorTemperatureResult = 'darkblue';}
...

编辑:您还可以制作一个范围的通用列表并从中处理:

var tempMap = {
  default: 'pink',
  ranges: [{
    min: -10,
    max: 0,
    color: 'midnightblue'
  }, {
    min: 0,
    max: 10,
    color: 'darkblue'
  }, {
    min: 10,
    max: 20,
    color: 'royalblue'
  }, {
    min: 20,
    max: 30,
    color: 'steelblue'
  }, {
    min: 30,
    max: 40,
    color: 'deepskyblue'
  }, {
    min: 40,
    max: 50,
    color: 'lightblue'
  }, {
    min: 50,
    max: 60,
    color: 'lightyellow'
  }, {
    min: 60,
    max: 70,
    color: 'lemonchiffron'
  }, {
    min: 70,
    max: 80,
    color: 'khaki'
  }, {
    min: 80,
    max: 90,
    color: 'orange'
  }, {
    min: 90,
    max: 100,
    color: 'gold'
  }, {
    min: 100,
    max: 110,
    color: 'orangered'
  }]
};

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

function colorTemperature(temperature) {
  var colorTemperatureResult = "";
  // its really cold out there
  if (temperature < tempMap.ranges[0].min) colorTemperatureResult = tempMap.ranges[0].color;
  else
    for (var i = 0; i < tempMap.ranges.length; i++) {
      if (between(temperature, tempMap.ranges[i].min, tempMap.ranges[i].max)) {
        colorTemperatureResult = tempMap.ranges[i].color;
        break;
      }
    }
  colorTemperatureResult = colorTemperatureResult ? colorTemperatureResult : tempMap.default;
  return colorTemperatureResult;
}

答案 4 :(得分:0)

要回答第二个问题,这将是一个更简单的方法,同时保持温度范围的概念。更新颜色/温度列表也更容易。您可以将范围分隔符(正斜杠)更改为您想要的任何内容:

var tempColorMap = {
    '-10/0'  : 'midnightblue',
    '1/10'   : 'darkblue',
    '11/20'  : 'royalblue',
    '21/30'  : 'steelblue',
    '31/40'  : 'deepskyblue',
    '41/50'  : 'lightblue',
    '51/60'  : 'lightyellow',
    '61/70'  : 'lemonchiffron',
    '71/80'  : 'khaki',
    '81/90'  : 'orange',
    '91/100' : 'gold',
    '101/500': 'orangered'
};

function getColorByTemp(temp) {
    var color = 'pink';

    for (var range in tempColorMap) {
        var rangeSplit = range.split('/');
        var min        = rangeSplit[0];
        var max        = rangeSplit[1];

        if (temp >= min && temp <= max) {
            color = tempColorMap[range];
            break;
        }
    }

    return color;
}

编辑:案例错误