表驱动法

2021-01-06  本文已影响0人  ticktackkk

阶梯访问表

用两个数组
0-59 分是不及格 F级
60- 79 是及格 E级
80-84 是普通 D级
85-89 是良好 C级
90 - 94 是优秀 B级
95-100 是太棒了 A级

最重要的是, 我的表里面是存放了 区间的上界 , 用来区分等级
普通方法是用if else 或者 switch case
现在换一种方法

const grade = ["F", "E", "D", "C", "B", "A"];
      const score = [59, 79, 84, 89, 94, 100];
      const getGrade = (someOneSorce) => {
        for (let i = 0; i < grade.length; i++) {
          if (someOneSorce < score[i]) {
            console.log(grade[i]);
            return grade[i];
          }
        }
      };
      getGrade(66);

直接访问表

计算今天是周几
普通为 case 0 :
today = "周日";
case 1 :
today = "周一";
case 2 :
today = "周二";
case 3 :
today = "周三";
case 4 :
today = "周四";
case 5 :
today = "周五";
default:
today = "周六";

            const weekDay = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
            const week = new Date().getDay();
            const taday = weekDay[week % 7];
            console.log(taday);

这个月有几天

      const allMonthDay = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
      const nowMonth = (month) => {
        console.log(allMonthDay[month - 1]);
      };
      nowMonth(2);
上一篇下一篇

猜你喜欢

热点阅读