Description
Solutions
Submission
REST API: Discounted Price
🔥 FULLTIME

Given a barcode, query the API at https://jsonmock.hackerrank.com/api/inventory?barcode=barcode and return the item's discounted price.

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 item's record.
  • In the data array, the item has the following schema:

  • barcode: the barcode for the product (String)
  • price: the gross selling price (Number)
  • discount: the discount percent to apply (Number).
  • Some fields that are not of interest.
  • page, per_page, total, total_pages, etc. are not required for this task.

    If the barcode is found, the data array contains exactly 1 element. If not, it is empty and the function should return '-1'.

    Use the "discount" and the "price" properties to calculate the discounted price rounded to the nearest integer.

    discountedPrice = price - ((discount / 100) * price)

    Function Description

    Complete the function getDiscountedPrice in the editor.

    getDiscountedPrice has the following parameters:

    • string barcode: the item to query

    Returns

    int: the discounted price rounded to the nearest integer or -1

    Constraints

    - There will be either 1 or 0 records in data.

    Example 1:

    Input:  barcode = "74002314"
    Output: 2964
    Explanation:
    First, a call is made to API https://jsonmock.hackerrank.com/api/inventory?barcode=74002314. The price = 3705 and discount = 20.
    Constraints:
      There will be either 1 or 0 records in data
    Thumbnail 0
    Testcase

    Result
    Case 1

    input:

    output: