Objective

Given a collection of prices of some items, output a table showing each price, the discount offered, and the price after the discount.

Difficulty

Easy

Description

You are given the following array, modeling the pricing of a couple of items in a shopping store:

<?php

$items = [[50, 30], [85, 10], [300, 15], [20, 5]];

Each item of this array is an array itself, consisting of two elements: the first is the actual price of the item (in $) in the store (without a discount) and the second is its discount, given as a percentage value.

For instance, [50, 30] means that the actual price of the item is $50 while the discount is 30%. If we compute the price after applying the discount, it turns out to be $35.

In this exercise, your job is to output a table (in the terminal) using this array, showing each item's actual price, its discount, and price after discount.

Here are a couple of things to abide by:

  • Each column should be of length 20, with a space in between, and the text within it should be left-aligned.
  • The headings of the table should be 'Price', 'Discount' and 'Price after discount', respectively.
  • After the table's header row, leave a blank line.
  • Furthermore, wherever prices are output, they should be to 2 decimal places and begin with the $ sign. For instance, if the price is 30, it should be output as $30.00; if the price is 50.789, it should be output as $50.79; and so on and so forth.
  • The discount should be output with a % sign at its end. For instance, if the discount is 20, the output should be 20%.

Shown below is the required output for the $items array shown above:

Price Discount Price after discount $50.00 30% $35.00 $85.00 10% $76.50 $300.00 15% $255.00 $20.00 5% $19.00

Hints