Time: 2022-07-08 17:40
    网易2203期《高级前端开发工程师》
    c44dd7f1859ff9430033f6da9000f98.png

    • 法一 — 利用绝对值边界控制变量
      1. for (let i = 0; i < 10; i++) {
      2. console.log(Math.abs((i + 3) % 6 - 3));
      3. }
      ```javascript let list = [ false, false, false, false ]; const list_change = function (newValue, oldValue = -1) { if (newValue > -1 && newValue < 4) list[newValue] = !list[newValue]; if (oldValue > -1 && oldValue < 4) list[oldValue] = !list[oldValue]; }

    // init point let point = oldPoint = -1; // current for (let i = 0; i < 10; i++) { oldPoint = point; point = Math.abs((i + 3) % 6 - 3); list_change(point, oldPoint); // console.log(list); }

    1. ```javascript
    2. let list = [ false, false, false, false ];
    3. const list_change = function (newValue, oldValue = -1) {
    4. if (newValue > -1 && newValue < 4) list[newValue] = !list[newValue];
    5. if (oldValue > -1 && oldValue < 4) list[oldValue] = !list[oldValue];
    6. }
    7. // init point, record the point
    8. let point = oldPoint = -1, record = 0;
    9. setInterval(() => {
    10. oldPoint = point;
    11. point = Math.abs((record + 3) % 6 - 3);
    12. list_change(point, oldPoint);
    13. ++record;
    14. // console.log(list);
    15. }, 500);
    • 法二 — 利用记录值判断变量操作方式
      1. let point = -1, record = 0;
      2. for (let i = 0; i < 10; i++) {
      3. point = record > 3 ? point - 1 : point + 1;
      4. record = record > 5 ? 0 : record + 1;
      5. console.log(point);
      6. }
      ```javascript let list = [ false, false, false, false ]; const list_change = function (newValue, oldValue = -1) { if (newValue > -1 && newValue < 4) list[newValue] = !list[newValue]; if (oldValue > -1 && oldValue < 4) list[oldValue] = !list[oldValue]; }

    // init point, record the point let point = oldPoint = -1, record = 0; for (let i = 0; i < 10; i++) { oldPoint = point; point = record > 3 ? point - 1 : point + 1; record = record > 5 ? 0 : record + 1; list_change(point, oldPoint); // console.log(list); }

    1. ```javascript
    2. let list = [ false, false, false, false ];
    3. const list_change = function (newValue, oldValue = -1) {
    4. if (newValue > -1 && newValue < 4) list[newValue] = !list[newValue];
    5. if (oldValue > -1 && oldValue < 4) list[oldValue] = !list[oldValue];
    6. }
    7. // init point, record the point
    8. let point = oldPoint = -1, record = 0;
    9. setInterval(() => {
    10. oldPoint = point;
    11. point = record > 3 ? point - 1 : point + 1;
    12. record = record > 5 ? 0 : record + 1;
    13. list_change(point, oldPoint);
    14. // console.log(list);
    15. }, 500);
    • 班副 ```javascript let fooArr = [true, true, true, true]

    const toggleShow = (arr, call, step = 1000, ) => { let index = 0 let toggle = null

    return setInterval(() => { if (typeof toggle === ‘boolean’) { arr[index] = !arr[index] toggle ? index ++ : index — } if (index % (arr.length -1) === 0) toggle = !toggle arr[index] = !arr[index] call(arr) }, step) }

    toggleShow(fooArr, (arg) => { console.log(arg) }) ```