-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculateTotalPrice.php
47 lines (36 loc) · 973 Bytes
/
CalculateTotalPrice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php include 'parts/headart.php'; ?>
<h1>Calculate Total Price</h1>
<h2>Code</h2>
<pre>
function calculateTotalPrice(array $prices, $discount)
{
$sum = 0;
sort($prices);
$rem = $discount / 100;
for ($i = 0; $i < count($prices); $i++) {
$sum = $sum + $prices[$i];
}
$total = $sum - $rem * $prices[count($prices) - 1];
return floor($total);
}
</pre>
<h2>Solution</h2>
<?php
function calculateTotalPrice(array $prices, $discount)
{
$sum = 0;
sort($prices);
$rem = $discount / 100;
for ($i = 0; $i < count($prices); $i++) {
$sum = $sum + $prices[$i];
}
$total = $sum - $rem * $prices[count($prices) - 1];
return floor($total);
}
$discount = 10;
echo '<br> Discount :' . $discount;
$prices = array(7, 13.5, 3, 8, 2, 1);
echo '<br> Prices :' . json_encode($prices);
echo '<br> CalculateTotalPrice :' . calculateTotalPrice($prices, $discount);
?>
<?php include 'parts/footerPart.php'; ?>