-
-
Notifications
You must be signed in to change notification settings - Fork 238
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
实现一个函数a,使其奇数次调用时返回1,偶数次调用时返回2(不能使用全局变量) #110
Comments
闭包应用 |
function main(){
let num = 0;
return function(){
return (++num) % 2 === 1 ? 1 : 2;
}
} |
function createFunction() {
let count = 0;
return function() {
count++;
if (count % 2 === 0) {
return 2;
} else {
return 1;
}
};
}
let a = createFunction();
console.log(a()); // 输出:1
console.log(a()); // 输出:2
console.log(a()); // 输出:1 |
|
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: