-
Notifications
You must be signed in to change notification settings - Fork 2
/
hw3.js
35 lines (31 loc) · 1.03 KB
/
hw3.js
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
// 輸入國家英文名,能夠查詢符合的國家資訊
const request = require('request');
const process = require('process');
const bookUrl = 'https://restcountries.eu/rest/v2/name';
const searchName = process.argv[2];
// 找國家
function searchCountries(name) {
request(
`${bookUrl}/${name}`,
(error, response, body) => {
if (response.statusCode >= 200 && response.statusCode < 300) {
try {
const countryInfo = JSON.parse(body);
const times = countryInfo.length;
for (let i = 0; i < times; i += 1) {
console.log('============');
console.log(`國家:${countryInfo[i].name}`);
console.log(`首都:${countryInfo[i].capital}`);
console.log(`貨幣:${countryInfo[i].currencies[0].code}`);
console.log(`國碼:${countryInfo[i].callingCodes[0]}`);
}
} catch (e) {
console.log(e);
}
} else {
console.log('找不到國家資訊');
}
},
);
}
searchCountries(searchName);