找出有限数列中和值最大的连续子数列

((list) => {
    let max = {
        total: 0,
        first: 0,
        last: 0,
    };
    let tmp = {
        total: 0,
        first: 0,
        last: 0,
    };
    let op = 0;//是否连续,0不连续,1递增连续,-1递减连续
    list.forEach((current, i) => {
        if (i === 0) {
            tmp.total = current;
            max.total = current;
            return;
        }
        const prev = list[i - 1];
        if (op === 0) {
            if (current > prev) {
                op = 1;
            } else if (current < prev) {
                op = -1;
            } else {
                op = 0;
            }
        }
        if (op !== 0 && current - prev === op) {
            tmp.total += current;
        } else {
            if (max.total < tmp.total) {
                max.total = tmp.total;
                max.first = tmp.first;
                max.last = tmp.last;
            }
            op = 0
            tmp.first = i;
            tmp.total = current;
        }
        tmp.last = i;
    });
    if (max.total < tmp.total) {
        max = tmp;
    }
    return [
        "和值:" + max.total,
        "值:" + list[max.first] + "~" + list[max.last],
        "索引:" + max.first + "~" + max.last
    ].join("\n");
})([1, 2, 3, 4, 3, 2, 5, 6, 7, 8]);

npm参数

npm init -y

-y 是初始化package.json的时候省略掉输入各种信息的步骤,都使用默认值

npm install module_name –save-dev

–save-dev 自动将依赖的模块加到devDependencies中

–save 自动将依赖的模块加到dependencies中

Chrome根据DOM属性加js断点

有的时候调页面样式时会莫名其妙出现错,比如某个东西不该显示的时候显示出来了。或者代码太多,不记得是哪个地方控制的这个地方。或者想看看别人页面的代码是怎么实现的。这时可以利用Chome的这个功能来调试。

1.选中要观察的DOM,右键”break on” -> “attribute modifications”来观察这个DOM的属性变化(一般情况下改变样式都是在改class和style属性)。

2.正常操作触发这个DOM的属性变化,当发生变化后会自动跳到断点初,这时看右边的call stack即可找到是哪个地方修改的属性。如果代码压缩了不方便看,可以点左下角的”{}”按钮来格式化代码。

 

AngularJS拦截$http请求

$httpProvider.interceptors.push(['$q',function ($q) {
    return {
        request: function (config) {
            if(config.url==='/admin.php') {
                return $q.reject('no auth!');
            }
            return config;
        },
        responseError: function(rejection) {
            if(rejection==='no auth!') {
                alert('无权限!!!');
                return {
                    data: {
                        errno: -1,
                        error:'无权限'
                    }
                }
            }
            return $q.reject(rejection);
        }
    }
}]);

flex-box布局上下分栏

.testbox{
  position: relative;
  height:300px;
  border: 4px solid green;
  width: 400px;
}
.flex-rows {
  display: flex;
  flex-direction: column;
  position: absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
}
.flex-rows nav{
  background-color: #ccc;
}
.flex-rows article {
  background-color: #cdc;
  height:100%;
  position: relative;
}
.flex-rows article > .scrollview {
  position: absolute;
  top:0;
  right:0;
  bottom:0;
  left:0;
  overflow: auto;
}
<div class="testbox">
  <div class="flex-rows">
    <nav>
      Nav.... height:auto
    </nav>
    <article>
      <div class="scrollview">
        <ol>
          <li>...</li><li>...</li><li>...</li><li>...</li><li>...</li>
          <li>...</li><li>...</li><li>...</li><li>...</li><li>...</li>
          <li>...</li><li>...</li><li>...</li><li>...</li><li>...</li>
          <li>...</li><li>...</li><li>...</li><li>...</li><li>...</li>
        </ol>
      </div>
    </article>
  </div>
</div>

See the Pen flex-box布局上下分栏 by Fan Yanan (@fanyanan) on CodePen.light

ISO8601时间格式

new Date('2016-10-17 10:00:00');在Safari下会被认为是Invalid Date。

需要按照标准的IOS8601格式,如:new Date('2016-10-17T10:00:00');

或严谨点儿带上时区的new Date('2016-10-17T10:00:00+08:00');