Alex has a list of items to purchase at a market. The owner offers to discount each item after the first one by the lowest marked price among the prior items. No item's price can be discounted below 0, and the list of items may not be reordered. Calculate the payable amount.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

[JAVASCRIPT] 

Complete the 'calculateAmount' function below. 

The function is expected to return a LONG_INTEGER 

The function accepts INTEGER_ARRAY prices as a paramter. 

---------------------------------------------------------------------------------------

function caluculateAmount(prices) { 

// Write your code here

}

 

 

1. Minimum Amount
Alex has a list of items to purchase at a market. The owner offers to discount each item
after the first one by the lowest marked price among the prior items. No item's price can
be discounted below 0, and the list of items may not be reordered. Calculate the payable
amount.
Example
prices = [2, 5, 1, 4]
Alex pays 2 for the first item since there are no previous items to compare to.
The second item costs 5-2 = 3.
The third item is free: max(1 - min(2, 5), 0) = max(-1, 0) = 0.
The fourth item costs 4 -1 = 3.
The total cost to purchase all items is 2+ 3 + 0 + 3 = 8.
The first item is never discounted and the minimum cost of any item is 0.
Function Description
Complete the function calculateAmountin the editor below. The function must return
Alex's total cost to purchase all the items.
calculateAmount has the following parameter(s):
int prices[n]: the original prices of each of the items selected
Returns:
int: the total cost to purchase the items after any discounts are applied
Transcribed Image Text:1. Minimum Amount Alex has a list of items to purchase at a market. The owner offers to discount each item after the first one by the lowest marked price among the prior items. No item's price can be discounted below 0, and the list of items may not be reordered. Calculate the payable amount. Example prices = [2, 5, 1, 4] Alex pays 2 for the first item since there are no previous items to compare to. The second item costs 5-2 = 3. The third item is free: max(1 - min(2, 5), 0) = max(-1, 0) = 0. The fourth item costs 4 -1 = 3. The total cost to purchase all items is 2+ 3 + 0 + 3 = 8. The first item is never discounted and the minimum cost of any item is 0. Function Description Complete the function calculateAmountin the editor below. The function must return Alex's total cost to purchase all the items. calculateAmount has the following parameter(s): int prices[n]: the original prices of each of the items selected Returns: int: the total cost to purchase the items after any discounts are applied
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY