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 trial
Sherwin Gaddis
3,267 PointsUser Defined Functions Code challenge
The challenge is this:
http://teamtreehouse.com/library/introducing-userdefined-functions
Challenge:
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, plus the return command to send the sum back to the main code.
My Code: for which the system says bummer - I get a wrong
<?php
function mimic_array_sum($array) {
$sum = 0;
foreach($array as $element) {
$sum = $sum + $element;
}
return $sum
}
$palindromic_primes = array(11, 757, 16361);
?>
What am I doing wrong?
Sherwin
8 Answers
Jessica Hawkins
535 PointsI am still having trouble with this my code reads like this...
<?php
function mimic_array_sum ($array) {
$sum=0;
foreach ($array as $element) {
$sum = $sum + $element;
}
return $sum;
}
$palindromic_primes = array(11, 757, 16361);
$sum = mimic_array_sum($palindrominc_primes);
echo $sum;
?>
and it is returning a value of 0. That is the correct order according to this thread...
UPDATE IM AN IDIOT! Spelling error - doy
Randy Hoyt
Treehouse Guest TeacherWhen you get the "Bummer" error, it means the code contains an error that makes it not valid PHP. In this case, it looks like you are missing a semi-colon at the end of one of the lines:
return $sum
Add a semi-colon to that and give it a try.
return $sum;
Saad Khan Malik
25,199 Pointsthis should help you all
<?php
///ans to part 1
function mimic_array_sum ($array) {
/// ans to part2
$sum=0;
foreach ($array as $element) {
$sum = $sum + $element;
};
return $sum;
};
$palindromic_primes = array(11, 757, 16361);
///ans to part 3
$sum = mimic_array_sum($palindromic_primes);
echo $sum;
?>
Sherwin Gaddis
3,267 PointsThat fixed it,
Thx!
Kari Jones
6,217 PointsI don't understand step 3 of this. Here is my code:
<?php
function mimic_array_sum($array) {
$sum = 0;
foreach($array as $element) {
$sum = $sum + $element;
}
return $sum
}
$sum = mimic_array_sum($palindromic_primes);
$palindromic_primes = array(11, 757, 16361);
echo $sum;
?>
Sherwin Gaddis
3,267 PointsKari,
I think if you take away this line
$sum = mimic_array_sum($palindromic_primes);
and
replace $array with $palindromic_primes
in the function. You should get it to work.
In the function statement
function mimic_array_sum($array)
the word $array is just a place holder to let you know that the function accepts one argument which is an array. You have to tell the function which array you want to pass in for it to work on and then it will pass back the finished value.
Hope this helps and Randy correct me if I am off somewhere.
Kari Jones
6,217 PointsThis did not work for me. When I swapped the $array in the function, it said that step 1 was incorrect and that I had to go back.
Sherwin Gaddis
3,267 PointsKari,
I am sorry that I mislead you. I had to go back and do the challenge myself to see what I missed. In your code, what I missed is the order. The order is incorrect. You can't use a variable before it is defined. In your code you are trying to use the variable $palindromic_primes before it is defined to the php interpreter.
Remember that the php interpreter is more strict than we are and looks and executes each line in order. If you have something out of order it will halt and throw an error. $sum should come after $palindromic_primes = so that it is defined first then applied to the next line $sum and then applied to the predefined function mimic.
I hope this helps. Regards,
Kari Jones
6,217 PointsThanks! I was hung up on that one code challenge for 2 days!
Jessica Hawkins
535 PointsThanks! I refuse to just copy and paste the example code when I can't figure something out. Won't get too far doing that will I? Appreciate your quick response!
Cassandra James
7,561 PointsI don't seem to be able to put 2 and 2 together
HOW did you know to add $sum = mimic_array_sum($palindromic_primes); echo $sum
All I understood was it was outside and even then I wasn't sure where it should go or what code would accomplish this after watching the video several times.
aracelialvarez
Courses Plus Student 2,493 Points"Can Someone Explain how to resolve this Problem I don't Understand what it wants me to do"?
In the main code, outside of the function, use the new mimic_array_sum() function you just wrote. Store the return value in a variable called $sum, and then display that sum to the screen.
Andrew Misplon
2,719 PointsHey Randy
Thanks for the great course!
It's perhaps worth taking an overall look at this thread. There are complete answers to the code challenges here - usually you guys pull those.
Randy Hoyt
Treehouse Guest TeacherRandy Hoyt
Treehouse Guest TeacherAh, I see the error now. (I looked at your code a few times, thinking that should it have been working.) Yes, with that spelling error, you pass an empty variable into the function and (without any elements to add together) it returns the 0. Good catch!