-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproduct.php
More file actions
32 lines (28 loc) · 727 Bytes
/
product.php
File metadata and controls
32 lines (28 loc) · 727 Bytes
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
<?php
// 8 kyu - Beginner - Reduce but Grow
// Given and array of integers (x), return the result of multiplying the values together in order. Example:
//
// [1, 2, 3] --> 6
// For the beginner, try to use the reduce method - it comes in very handy quite a lot so is a good one to know.
//
// Array will not be empty.
function grow($a) {
$result = 1;
for ($i = 0; $i < count($a); $result *= $a[$i++]) {}
return $result;
}
// Alternative Solutions:
// function grow($a) {
//
// if ( !empty($a) ) {
// return $result = array_product($a);
// }
// }
// function product($carry, $item) {
// $carry *= $item;
// return $carry;
// }
// function grow($a) {
// return array_reduce($a, "product", 1);
// }
?>