-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcube_odd.php
More file actions
37 lines (31 loc) · 880 Bytes
/
cube_odd.php
File metadata and controls
37 lines (31 loc) · 880 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
33
34
35
36
37
<?php
// 7 kyu - Sum of Odd Cubed Numbers
// Find the sum of the odd numbers within an array, after cubing the initial integers. This function will return undefined (NULL in PHP) if any of the values aren't numbers.
function cube($n) {
return pow($n, 3);
};
function is_odd($n) {
return $n % 2 !== 0;
};
function cube_odd($a) {
return count(array_filter($a, 'is_numeric')) === count($a)
? array_sum(array_map('cube', array_filter($a, 'is_odd')))
: null;
};
// Alternative Solutions:
// function cube_odd($a) {
// $sum = 0;
// foreach ($a as $number) {
// if (!is_numeric($number)) {
// return null;
// }
// if ($number & 1) {
// $sum += ($number ** 3);
// }
// }
//
// return $sum;
// }
$answer = cube_odd([1, 2, 3, 4]);
print_r("$answer \n");
?>