-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
95 lines (75 loc) · 3.04 KB
/
app.js
File metadata and controls
95 lines (75 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Lab 1: การใช้งาน Node.js เบื้องต้น
console.log('=== Lab 1: Node.js พื้นฐาน ===\n');
// 1. การแสดงผลพื้นฐาน
console.log('1. การแสดงผลพื้นฐาน:');
console.log('สวัสดี Node.js!');
console.log('Hello Node.js!');
console.log('เวอร์ชั่น Node.js:', process.version);
console.log('แพลตฟอร์ม:', process.platform);
console.log();
// 2. การทำงานกับตัวแปรและ data types
console.log('2. ตัวแปรและ Data Types:');
let name = 'สมชาย';
const age = 25;
var isStudent = true;
console.log(`ชื่อ: ${name}`);
console.log(`อายุ: ${age} ปี`);
console.log(`เป็นนักเรียน: ${isStudent}`);
// Array และ Object
const fruits = ['แอปเปิล', 'กล้วย', 'ส้ม'];
const person = {
name: 'สมหญิง',
age: 23,
city: 'กรุงเทพ'
};
console.log('ผลไม้:', fruits);
console.log('ข้อมูลบุคคล:', person);
console.log();
// 3. Functions
console.log('3. การใช้งาน Functions:');
function greet(name) {
return `สวัสดี ${name}!`;
}
const calculateArea = (width, height) => {
return width * height;
};
console.log(greet('น้องมิ้น'));
console.log(`พื้นที่สี่เหลี่ยม 5x3 = ${calculateArea(5, 3)}`);
console.log();
// 4. การจัดการ Error พื้นฐาน
console.log('4. การจัดการ Error:');
try {
let result = 10 / 0;
if (!isFinite(result)) {
throw new Error('ไม่สามารถหารด้วย 0 ได้');
}
console.log('ผลลัพธ์:', result);
} catch (error) {
console.log('เกิดข้อผิดพลาด:', error.message);
}
console.log();
// 5. การใช้งาน setTimeout (Asynchronous)
console.log('5. Asynchronous Programming:');
console.log('เริ่มต้นการทำงาน...');
setTimeout(() => {
console.log('ข้อความนี้จะแสดงหลังจาก 2 วินาที');
}, 2000);
setTimeout(() => {
console.log('ข้อความนี้จะแสดงหลังจาก 1 วินาที');
}, 1000);
console.log('สิ้นสุดการทำงาน synchronous (แต่ async ยังทำงานอยู่)');
console.log();
// 6. การทำงานกับ JSON
console.log('6. การทำงานกับ JSON:');
const student = {
id: 1,
name: 'นายพิชิต',
subjects: ['คณิตศาสตร์', 'วิทยาศาสตร์', 'ภาษาอังกฤษ'],
gpa: 3.75
};
const jsonString = JSON.stringify(student);
console.log('JSON String:', jsonString);
const parsedObject = JSON.parse(jsonString);
console.log('Parsed Object:', parsedObject);
console.log();
console.log('=== จบ Lab 1 ===');