REST API: Country Codes
Learn this problemProblem statement
Given a country name and a phone number, query the API at
https://jsonmock.hackerrank.com/api/countries?name=country to get the country's
calling codes. Prepend the calling code to the phone number and return the string. If the data
array is empty, return the string '-1'. If there are multiple calling codes, use the one at the
highest index. The format of the number should be:
<Calling Code><space><Phone Number>
Example:
+1 7653555443
The response is a JSON object with 5 fields. The essential field is data:
In the data array, the country has the following schema:
page, per_page, total, total_pages, etc. are not required for this task.
If the country is found, the data array contains exactly 1 element. If not, it is empty and the function should return '-1'.
Function
getPhoneNumbers(country: String, phoneNumber: String) → String
Complete the getPhoneNumbers function in the editor.
getPhoneNumbers has the following parameters:
- string country: the country to query
- string phoneNumber: the phone number
Returns
string: the completed phone number or -1
Examples
Example 1
country = "Afghanistan"phoneNumber = "6564454455"return = "+93 6564454455"https://jsonmock.hackerrank.com/api/countries?name=Afghanistan.
The calling codes array contains 1 entry, '93'.Example 2
country = "Puerto Rico"phoneNumber = "564539386"return = "+1939 564539386"Example 3
country = "Oceania"phoneNumber = "987574876"return = "-1"