Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNeslee Rodillo
19,615 PointsI don't understand part 2 and I get easily confused on user defined functions, I'm using the return command on the video
Question: Challenge task 2 of 3 Write the code inside this mimic_array_sum() function. That code should add up all individual numbers in the array and return the sum. You’ll need to use a foreach command to loop through the argument array, a working variable to keep the running total, and a return command to send the sum back to the main code.
My answer:
function mimic_array_sum($array) {
$sum = 0;
foreach($numbers as $number) {
$sum = $sum + $number;
}
return $sum;
}
$palindromic_primes = array(11, 757, 16361);
$sum = mimic_array_sum($palindromic_primes);
echo $um;
?>
Error: Bummer! It doesn't look like the mimic_array_sum function is returning a value. Make sure you are using the return command.
2 Answers
Stone Preston
42,016 Pointsthe problem is the foreach loop. you did not use the name of the parameter, you used a different name.
below is the function header you wrote:
function mimic_array_sum($array) {
notice that the name of the parameter is $array
in your for each loop you have
foreach($numbers as $number) {
you need to use the name of the parameter (you called it $array):
foreach($array as $number) {
other than that your code looks good for task 2. (you misspelled sum towards the bottom but it shouldnt matter for task 2) I passed with the following
<?php
function mimic_array_sum($array) {
$sum = 0;
foreach($array as $number) {
$sum = $sum + $number;
}
return $sum;
}
$palindromic_primes = array(11, 757, 16361);
?>
Neslee Rodillo
19,615 PointsHi Carrie Lones,
Thank you for your explanation, programming is not really my strong point so therefore functions, variables are confusing. Though, your explanation was clear and understandable.
Treehouse is the best.
Neslee Rodillo
19,615 PointsNeslee Rodillo
19,615 PointsHi Stone,
Programming is not my strong suit.
Thank you soo much for your breakdown and explanation, it has helped alot. The part I was confused on was the for each loop: foreach($array as $number), this part was confusing because there are two terms to describe the same element.
$palindromic_primes outside the function $array inside the function. Which both describe the list of numbers.
also, which terms to use for the for each loop, ($array as $number) which really threw me. But your examples are great, you should be one of the teachers on treehouse