Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây.Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn.Tìm hiểu thêm về các ký tự unicode hai chiều
// Môi trường NodeJS của HackerRank cho phép gói `request`. | |
constrequest=require['request']; request = require['request']; | |
functionfetch[url]{ fetch[url] { | |
returnnewPromise[[resolve,reject]=>{ new Promise[[resolve, reject] => { | |
request[url,function[error,response,body]{[url, function [error, response, body] { | |
if[error] [error] | |
reject[error][error] | |
else | |
resolve[body][body] | |
}];]; | |
}];]; | |
} | |
asyncfunctiongetCountryName[countryCode]{ function getCountryName[countryCode] { | |
letpageNumber=1; pageNumber = 1; | |
letcountryName=null; countryName = null; | |
while[countryName===null]{ [countryName === null] { | |
leturl=`//jsonmock.hackerrank.com/api/countries?page=${pageNumber}`; url = `//jsonmock.hackerrank.com/api/countries?page=${pageNumber}`; | |
response=awaitfetch[url]; = await fetch[url]; | |
responseBody=JSON.parse[response]; = JSON.parse[response]; | |
responseBody.data.forEach[[countryData]=>{.data.forEach[[countryData] => { | |
if[countryData.alpha2Code===countryCode] [countryData.alpha2Code === countryCode] | |
countryName=countryData.name; = countryData.name; | |
}];]; | |
if[responseBody.total_pages==pageNumber++] [responseBody.total_pages == pageNumber++] | |
break;; | |
} | |
if[countryName===null]{ [countryName === null] { | |
Thrownewerror ["Mã quốc gia không tìm thấy."]; new Error["Country code not found."]; | |
} | |
returncountryName; countryName; | |
} | |
getCountryName["AF"].then[[result]=>{["AF"].then[[result] => { | |
console.log[result];.log[result]; | |
}];]; | |
getCountryName["ZW"].then[[result]=>{["ZW"].then[[result] => { | |
console.log[result];.log[result]; | |
}];]; | |
getCountryName["NaN"].then[[result]=>{["NaN"].then[[result] => { | |
console.log[result];.log[result]; | |
}].catch[[error]=>{].catch[[error] => { | |
console.log[error.message];.log[error.message]; | |
}];]; |