-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (126 loc) · 3.35 KB
/
script.js
File metadata and controls
151 lines (126 loc) · 3.35 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
print("--- BASICS ---");
var arr = [10, 20, 30, 40, 50];
print("Array:", arr);
arr.push(60);
print("After push(60):", arr);
arr.push(70, 80);
print("After push(70, 80):", arr);
print("arr[0]:", arr[0]);
print("arr[2]:", arr[2]);
print("arr[4]:", arr[4]);
print("arr.length:", arr.length);
print("Out of bounds arr[10]:", arr[10]);
print("Negative index arr[-1]:", arr[-1]);
var nested = [[1, 2], [3, 4]];
print("Nested array:", nested);
print("nested[0]:", nested[0]);
print("nested[0][1]:", nested[0][1]);
let obj = { a: 1, b: 2, c: 3 };
print(obj);
var squared = arr.map(function(x) { return x * x; });
print("Squared:", squared);
var greaterThan3 = arr.filter(function(x) { return x > 3; });
print("Greater than 3:", greaterThan3);
var lessThan3 = arr.filter(function(x) { return x < 3; });
print("Less than 3:", lessThan3);
var result = arr
.map(function(x) { return x * 2; })
.filter(function(x) { return x > 40; })
.map(function(x) { return x + 10; });
print("Chain result:", result);
var x = 10;
var y = 20;
var sum = x + y;
print("x =", x, ", y =", y, ", x + y =", sum);
var greeting = "Hello";
var name = "World";
var message = greeting + ", " + name + "!";
print(message);
var multiply = function(a, b) {
return a * b;
};
var result = multiply(6, 7);
print("6 * 7 =", result);
var age = 25;
if (age >= 18) {
print("You are an adult");
} else {
print("You are a minor");
}
print("Counting from 1 to 5:");
var i = 1;
while (i <= 5) {
print(i);
i = i + 1;
}
var person = {
name: "Alice",
age: 30,
city: "New York"
};
print("Name:", person.name);
print("Age:", person.age);
print("City:", person.city);
var factorial = function(n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
};
print("factorial(5) =", factorial(5));
var makeCounter = function() {
var count = 0;
return function() {
count = count + 1;
return count;
};
};
var counter = makeCounter();
print("counter() =", counter());
print("counter() =", counter());
print("true:", true);
print("false:", false);
print("!true:", !true);
print("!false:", !false);
print("5 == 5:", 5 == 5);
print("5 != 3:", 5 != 3);
print("10 > 5:", 10 > 5);
print("3 < 7:", 3 < 7);
print("--- /BASICS ---");
print("--- fetch/JSON ---");
print("=== GET List of Posts ===")
let response = fetch("https://jsonplaceholder.typicode.com/posts")
print("Status:", response.status)
print("OK:", response.ok)
print("Body length:", response.body)
print("")
print("=== POST Request with JSON Body ===")
let response2 = fetch("https://jsonplaceholder.typicode.com/posts", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({"title": "My Post", "body": "This is a test post", "userId": 1})
})
print("Status:", response2.status)
print("OK:", response2.ok)
print("Body:", response2.body)
print("")
print("=== Request with Custom Headers ===")
let response3 = fetch("https://jsonplaceholder.typicode.com/posts/1", {
headers: {
"Accept": "application/json",
"User-Agent": "GoScript/1.0"
}
})
print("Status:", response3.status)
print("OK:", response3.ok)
print("Response Headers:", response3.headers)
print("")
print("=== Error Handling - Invalid URL ===")
let response4 = fetch("not-a-valid-url")
if (response4.error) {
print("Error:", response4.error)
}
print("")
print("--- /fetch/JSON ---");