添加和减去数组范围

时间:2017-02-04 18:19:37

标签: javascript arrays range

嘿,我对这段代码有疑问:

var levelsRange = arrayeventslevel[0] + " through " +  arrayeventslevel[arrayeventslevel.length-1]; ; 
$("#existorders").html(
  "There are currently: " + arrayeventslength.length + 
  " events on " + dayoftheweek + 
  "<br/>" + " with order levels: " + levelsRange + 
  "<br />" + "You can move new event to levels ranging between: " + newLevelsRange
);

当前levelsRange输出,例如1到6范围。如果是这样的话, 我需要另一个变量newLevelsRange,根据初始变量范围应该说0到7。 但是,如果levelsRange表示0到6,则新变量应该表示0到7 NOT -1到7

我无法从初始变量信息中正确添加减法。有人可以帮忙。

2 个答案:

答案 0 :(得分:1)

var newLevelsRange=(arrayeventslevel[0]||1)-1 + " through " +  (arrayeventslevel[arrayeventslevel.length-1]-1);

只需检查第一个元素是否为零,如果是,则取1 ...

答案 1 :(得分:0)

我刚刚做了这个

 if(arrayeventslevel[0] != 0){
                            arrayeventslevel[0] = arrayeventslevel[0] - 1;
                        arrayeventslevel.length  =arrayeventslevel.length + 1;
                     }

                     var newLevelRange = arrayeventslevel[0] + " through " +  arrayeventslevel.length;

不确定如何使用Math.max