# 一而再，再而三（二）

    if (code >= 16 && code <= 30) {
termCode = '30d'
}
........
if (code >= 331 && code <= 365) {
termCode = '365d'
}

    const dayRanges = [15, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 365];
if (code < dayRanges[0] || code > dayRanges[dayRanges.length - 1]) {
throw new Error('请选择正确的时间范围' );
}
for (let i = 0; i < dayRanges.length; i++) {
if ((code >= (dayRanges[i] + 1)) && (code <= dayRanges[i + 1])) {
termCode = dayRanges[i + 1] + 'd';
break;
}
}

exports.termCode = function(dayRanges, code) {
if (code < dayRanges[0] || code > dayRanges[dayRanges.length - 1]) {
throw new Error('请选择正确的时间范围' );
}
for (let i = 0; i < dayRanges.length; i++) {
if ((code >= (dayRanges[i] + 1)) && (code <= dayRanges[i + 1])) {
termCode = dayRanges[i + 1] + 'd';
break;
}
}
};
module.exports = exports;
const lib = require('./lib');

const dayRanges = [15, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 365];
const code = 28;
const termCode = lib. termCode(dayRanges, code);

posted @ 2018-05-05 22:38 x3d 阅读(...) 评论(...) 编辑 收藏