feat: Add JavaScript core exercises and solutions

- Implemented exercises for array high-order methods, memory and execution, switch statements, and final review.
- Added starter and answer files for each exercise to facilitate learning.
- Created a runner HTML file to execute JavaScript code and display console outputs.
- Updated README files to include exercise objectives, tasks, and usage instructions.
This commit is contained in:
charlie
2026-03-13 11:09:19 +08:00
parent 4495ae0e28
commit 877acb5a8f
51 changed files with 1949 additions and 5 deletions

View File

@@ -0,0 +1,28 @@
# 练习 9闭包入门
## 目标
初步理解闭包为什么能把外层变量“记住”。
## 你要练什么
- 外层变量
- 内层函数
- 闭包的基本效果
- 多个闭包实例互不影响
## 任务
请完成一个“计数器工厂”脚本,要求:
- 写一个 `createCounter` 函数
- 在函数内部定义 `count`
- 返回一个内部函数
- 每次调用内部函数时,`count` 都加 1
- 创建两个不同的计数器
- 观察为什么它们各自记住了自己的 `count`
## 文件
- [starter.js](/Users/lijiaqing/home/wwwroot/front-end-example/03-javascript-core/09-scope-and-closure/starter.js)
- [answer.js](/Users/lijiaqing/home/wwwroot/front-end-example/03-javascript-core/09-scope-and-closure/answer.js)

View File

@@ -0,0 +1,16 @@
function createCounter() {
let count = 0;
return function () {
count += 1;
return count;
};
}
const counterA = createCounter();
const counterB = createCounter();
console.log("counterA 第一次:", counterA());
console.log("counterA 第二次:", counterA());
console.log("counterB 第一次:", counterB());
console.log("每次调用 createCounter 都会创建一个新的闭包环境。");

View File

@@ -0,0 +1,11 @@
function createCounter() {
let count = 0;
// 返回一个函数
}
// 任务:
// 1. 创建 counterA 和 counterB
// 2. 连续调用 counterA 两次
// 3. 再调用 counterB 一次
// 4. 观察为什么两个计数器互不影响