Description
Solutions
Submission
REST API: Country Codes
🤘 INTERN

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:

  • data: Either an empty array or an array with a single object that contains the country's record.
  • In the data array, the country has the following schema:

  • name: The name of the country (String)
  • callingCodes: An array of the country's calling codes (String Array)
  • A number of fields that are not of interest.
  • 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 Description

    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

    Example 1:

    Input:  country = "Afghanistan", phoneNumber = "6564454455"
    Output: "+93 6564454455"
    Explanation:
    A call is made to API https://jsonmock.hackerrank.com/api/countries?name=Afghanistan. The calling codes array contains 1 entry, '93'.

    Example 2:

    Input:  country = "Puerto Rico", phoneNumber = "564539386"
    Output: "+1939 564539386"
    Explanation:
    A call is made to the API to fetch the record for Puerto Rico. The returned callingCodes = ['1787', '1939']. Use the higher index code, callingCodes[1].

    Example 3:

    Input:  country = "Oceania", phoneNumber = "987574876"
    Output: "-1"
    Explanation:
    The API call return has an empty data array.
    Constraints:
    • The returned JSON object contains either 0 or 1 record in data.
    • he country name may contain uppercase and lowercase English letters and <space> (ascii 32)
    Thumbnail 0
    Testcase

    Result
    Case 1

    input:

    output: