Time: 2022-07-08 17:40
网易2203期《高级前端开发工程师》
- 法一 — 利用绝对值边界控制变量
```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]; }for (let i = 0; i < 10; i++) {console.log(Math.abs((i + 3) % 6 - 3));}
// 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); }
```javascriptlet 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 pointlet point = oldPoint = -1, record = 0;setInterval(() => {oldPoint = point;point = Math.abs((record + 3) % 6 - 3);list_change(point, oldPoint);++record;// console.log(list);}, 500);
- 法二 — 利用记录值判断变量操作方式
```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]; }let point = -1, record = 0;for (let i = 0; i < 10; i++) {point = record > 3 ? point - 1 : point + 1;record = record > 5 ? 0 : record + 1;console.log(point);}
// 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); }
```javascriptlet 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 pointlet point = oldPoint = -1, record = 0;setInterval(() => {oldPoint = point;point = record > 3 ? point - 1 : point + 1;record = record > 5 ? 0 : record + 1;list_change(point, oldPoint);// console.log(list);}, 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) }) ```
