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:
30
03-javascript-core/13-array-high-order-methods/README.md
Normal file
30
03-javascript-core/13-array-high-order-methods/README.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# 练习 13:数组高阶函数
|
||||
|
||||
## 目标
|
||||
|
||||
学会使用数组高阶函数处理一组数据。
|
||||
|
||||
## 你要练什么
|
||||
|
||||
- `map()`
|
||||
- `filter()`
|
||||
- `reduce()`
|
||||
- `find()`
|
||||
- `some()`
|
||||
- `every()`
|
||||
|
||||
## 任务
|
||||
|
||||
请完成一个“学习成绩分析”脚本,要求:
|
||||
|
||||
- 用 `map` 生成带单位的新数组
|
||||
- 用 `filter` 找出及格成绩
|
||||
- 用 `reduce` 计算总分
|
||||
- 用 `find` 找出第一条大于等于 90 的成绩
|
||||
- 用 `some` 判断是否有人不及格
|
||||
- 用 `every` 判断是否全部完成考试
|
||||
|
||||
## 文件
|
||||
|
||||
- [starter.js](/Users/lijiaqing/home/wwwroot/front-end-example/03-javascript-core/13-array-high-order-methods/starter.js)
|
||||
- [answer.js](/Users/lijiaqing/home/wwwroot/front-end-example/03-javascript-core/13-array-high-order-methods/answer.js)
|
||||
37
03-javascript-core/13-array-high-order-methods/answer.js
Normal file
37
03-javascript-core/13-array-high-order-methods/answer.js
Normal file
@@ -0,0 +1,37 @@
|
||||
const scores = [58, 76, 91, 84];
|
||||
const students = [
|
||||
{ name: "小周", finished: true },
|
||||
{ name: "小林", finished: true },
|
||||
{ name: "小陈", finished: false },
|
||||
];
|
||||
|
||||
const scoreLabels = scores.map(function (score) {
|
||||
return `${score}分`;
|
||||
});
|
||||
|
||||
const passedScores = scores.filter(function (score) {
|
||||
return score >= 60;
|
||||
});
|
||||
|
||||
const totalScore = scores.reduce(function (total, score) {
|
||||
return total + score;
|
||||
}, 0);
|
||||
|
||||
const topScore = scores.find(function (score) {
|
||||
return score >= 90;
|
||||
});
|
||||
|
||||
const hasFailedScore = scores.some(function (score) {
|
||||
return score < 60;
|
||||
});
|
||||
|
||||
const allFinished = students.every(function (student) {
|
||||
return student.finished === true;
|
||||
});
|
||||
|
||||
console.log("带单位成绩:", scoreLabels);
|
||||
console.log("及格成绩:", passedScores);
|
||||
console.log("总分:", totalScore);
|
||||
console.log("第一个 90 分以上:", topScore);
|
||||
console.log("是否存在不及格:", hasFailedScore);
|
||||
console.log("是否全部完成:", allFinished);
|
||||
14
03-javascript-core/13-array-high-order-methods/starter.js
Normal file
14
03-javascript-core/13-array-high-order-methods/starter.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const scores = [58, 76, 91, 84];
|
||||
const students = [
|
||||
{ name: "小周", finished: true },
|
||||
{ name: "小林", finished: true },
|
||||
{ name: "小陈", finished: false },
|
||||
];
|
||||
|
||||
// 任务:
|
||||
// 1. 用 map 生成 ["58分", ...]
|
||||
// 2. 用 filter 筛出及格分
|
||||
// 3. 用 reduce 计算总分
|
||||
// 4. 用 find 找到第一个 >= 90 的分数
|
||||
// 5. 用 some 判断是否存在不及格
|
||||
// 6. 用 every 判断 students 是否都 finished 为 true
|
||||
Reference in New Issue
Block a user