-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw3.js
31 lines (27 loc) · 883 Bytes
/
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
/* eslint indent: 0 */
const request = require('request');
const process = require('process');
const apiUrl = 'https://restcountries.eu/rest/v2/name/';
const nationName = process.argv[2];
request(`${apiUrl}${nationName}`, (error, response, body) => {
if (error) {
console.log('error');
return;
}
const data = JSON.parse(body);
if (data.status >= 400 && data.status < 500) {
console.log('「找不到國家資訊」');
return;
}
if (!nationName) {
console.log('請輸入國家名稱');
return;
}
for (let i = 0; i < data.length; i += 1) {
console.log('========================');
console.log(`國家: ${data[i].name}`);
console.log(`首都: ${data[i].capital}`);
console.log(`貨幣: ${data[i].currencies[0].code}`);
console.log(`國碼: ${data[i].callingCodes[0]}`);
}
});