Files
front-end-example/03-javascript-core/15-switch-break-and-empty-values/starter.js
2026-03-23 14:56:04 +08:00

48 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const learningStatus = "review";
const records = ["变量", "条件", "函数", null, "对象"];
let optionalNote;
const finalComment = null;
let statusText = "";
const finishedRecords = [];
switch (learningStatus) {
case 'review':
console.log('已学习')
break
case 'ing':
console.log('学习中');
break
case 'prepare':
console.log('未学习');
break
default:
console.log('状态未知');
break
}
console.log(optionalNote, finalComment);
for (let i = 0; i < records.length; i++) {
if (records[i] === null || records[i] === undefined) {
console.log(`${i + 1}项为空值,停止读取`)
break
} else {
finishedRecords.push(records[i])
}
}
console.log(finishedRecords)
// 任务:
// 1. 用 switch 给 learningStatus 生成说明文字
// 2. 输出 optionalNote 和 finalComment 分别是什么
// 3. 用 for 循环读取 records
// 4. 如果遇到 undefined 或 null就 break
// 5. 输出 finishedRecords
/*用 `switch` 根据学习状态输出不同说明
- 观察 `undefined` 和 `null` 的区别
- 用循环读取学习记录
- 如果读到 `undefined` 或 `null`,立即用 `break` 停止循环
- 输出停止前已经读取到的内容*/