Restructure the training guide into focused tasks with warmed-up basics and updated exercises, and drop practice2.ts from the project tree.
67 lines
1.4 KiB
TypeScript
67 lines
1.4 KiB
TypeScript
const hello = "Hello, Zed Vim!";
|
|
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
|
|
function greet(name: string): string {
|
|
return `Hello, ${name}! Welcome to Vim mode.`;
|
|
}
|
|
|
|
function sumArray(arr: number[]): number {
|
|
let total = 0;
|
|
for (let i = 0; i < arr.length; i++) {
|
|
total += arr[i];
|
|
}
|
|
return total;
|
|
}
|
|
|
|
class User {
|
|
name: string;
|
|
email: string;
|
|
isActive: boolean;
|
|
|
|
constructor(name: string, email: string) {
|
|
this.name = name;
|
|
this.email = email;
|
|
this.isActive = true;
|
|
}
|
|
|
|
deactivate(): void {
|
|
this.isActive = false;
|
|
}
|
|
|
|
getDisplayName(): string {
|
|
return `${this.name} <${this.email}>`;
|
|
}
|
|
}
|
|
|
|
const user = new User("Alice", "alice@example.com");
|
|
console.log(greet(user.name));
|
|
console.log("Sum:", sumArray(numbers));
|
|
console.log("Active:", user.isActive);
|
|
|
|
const users = [
|
|
{ id: 1, name: "Alice", age: 30 },
|
|
{ id: 2, name: "Bob", age: 17 },
|
|
{ id: 3, name: "Carol", age: 25 },
|
|
];
|
|
|
|
function getUserNames(list: any[]) {
|
|
const names = [];
|
|
for (const item of list) {
|
|
names.push(item.nam); // ← 问题 2:拼写错误,nam 应该是 name
|
|
}
|
|
return names;
|
|
}
|
|
|
|
function getAdultUsers(list: any[]) {
|
|
return list.filter((u) => u.age >= 18);
|
|
}
|
|
|
|
console.log("debug: users =", users); // ← 问题 1:调试残留,上线前删掉
|
|
|
|
const names = getUserNames(users);
|
|
const adults = getAdultUsers(users);
|
|
console.log(names);
|
|
console.log(adults);
|
|
|
|
export {};
|