Pizza Price Calculator
Problem statement
Write a function that works out the price of a pizza from its size, its toppings, and whether it has a premium crust.
The price follows these rules:
sizesets the base price:smallis5,mediumis10, andlargeis15. The name must match exactly.- Each entry in
toppingsadds2, even when the same topping appears more than once.toppingscan be empty. - When
premiumCrustistrue, add5.
Return the price. If size is not one of the three names, return -1.0.
Function
calculatePizzaPrice(size: String, toppings: String[], premiumCrust: boolean) → doubleExamples
Example 1
size = "medium"toppings = ["mushroom","onion"]premiumCrust = truereturn = 19.0A medium pizza is 10, two toppings add 4, and the premium crust adds 5: 10 + 4 + 5 = 19.
Example 2
size = "small"toppings = ["pepper"]premiumCrust = falsereturn = 7.0A small pizza is 5, one topping adds 2, and there is no premium crust: 5 + 2 = 7.
Example 3
size = "large"toppings = ["ham","olive","basil","corn"]premiumCrust = truereturn = 28.0A large pizza is 15, four toppings add 8, and the premium crust adds 5: 15 + 8 + 5 = 28.
Example 4
size = "huge"toppings = ["ham"]premiumCrust = truereturn = -1.0huge is not one of the three size names, so the answer is -1.0, whatever else was ordered.
Constraints
0 <= size.length <= 10sizecontains only printable ASCII characters.0 <= toppings.length <= 1001 <= toppings[i].length <= 20toppings[i]contains only lowercase English letters.premiumCrustistrueorfalse.