From 93e6e2d6b6902c15adf1e9459a57050185c80422 Mon Sep 17 00:00:00 2001 From: Lu-yeom <82422161+Lu-yeom@users.noreply.github.com> Date: Sun, 9 May 2021 14:26:57 +0800 Subject: [PATCH] self-learning by Luyeom-week4_hw3 --- homeworks/week4/hw3.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/homeworks/week4/hw3.js b/homeworks/week4/hw3.js index e69de29..7be56f1 100644 --- a/homeworks/week4/hw3.js +++ b/homeworks/week4/hw3.js @@ -0,0 +1,28 @@ +const request = require('request'); +const args = process.argv; + +const country = args[2]; + +if (!country) { + return console.log('Please enter country name'); +} + +request( + 'https://restcountries.eu/rest/v2/name/{name}', + function (error,response,body) { + if (error) { + return console.log('存取錯誤', error); + } + const data = JSON.parse(body); + if (data.status === 404) { + return console.log('cannot find country') + } + + for (let i = 0; i < data.length; i++) { + 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]); + } +})