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).
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 APIhttps://jsonmock.hackerrank.com/api/inventory?barcode=74002314
. Theprice = 3705
anddiscount = 20
.
There will be either 1 or 0 records in data

input:
output: