-
Notifications
You must be signed in to change notification settings - Fork 896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
设计一个函数,奇数次执行的时候打印 1,偶数次执行的时候打印 2 #162
Labels
Comments
|
function countFn() {
let count = 0;
return function (...args) {
count++;
if (count & 1 === 1) return console.log(1);
console.log(2);
}
}
const testFn = countFn();
testFn(); // 1
testFn(); // 2
testFn(); // 1
testFn(); // 2
testFn(); // 1
testFn(); // 2 |
function* Print() {
while (true) {
yield console.log('1');
yield console.log('2');
}
}
let p = Print()
function myprint() {
p.next()
}
myprint()
myprint()
myprint()
} |
|
最直接的思路是使用闭包 const oddEvenConsole = () => {
let count = 0;
return function () {
const isEven = count % 2 === 0;
console.log(isEven ? 2 : 1);
count++;
};
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: