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 trialChase Snow
32,932 PointsConfused
I am not really sure why this is wrong, any help would be much appreciated!
5 Answers
Andrew Shook
31,709 PointsOk i found your problem. Hint- In the function look at your return statement.
Chase Snow
32,932 PointsWhoops..
This is the question the code challenge asks;
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.
this is what I have and it appears to be incorrect;
<?php
function mimic_array_sum($array) {
$sum = 0;
foreach($array as $number) {
$sum = $sum + $number;
}
return $number;
}
$palindromic_primes = array(11, 757, 16361);
$total = mimic_array_sum($palindromic_primes);
?>
Jeff Busch
19,287 PointsHi Chase,
If you look to the bottom right of the text box you will see something that says Markdown Cheetsheet. If you don't understand that maybe this will make sense: How to display code at Treehouse
Jeff
Chase Snow
32,932 PointsAh I see, thank you for all the advice, I have been on the site few months but haven't used the forum yet, your help is appreciated
<?php
function mimic_array_sum($array) {
$sum = 0; foreach($array as $number) {
$sum = $sum + $number;
}
return $number;
}
$palindromic_primes = array(11, 757, 16361);
$total = mimic_array_sum($palindromic_primes);
echo $total;
?>
Robert Walker
17,146 PointsHi Chase,
You have one thing wrong:
<?php
function mimic_array_sum($array) {
$sum = 0; foreach($array as $number) {
$sum = $sum + $number;
}
return $number;
}
?>
In the above code you are returning the $number but the function is adding $sum + $number and placing that result into $sum.
So you should have $sum as the return.
Without checking I assume the current result would only return the last number in the array 16361.
The below code should solve your problem, it is easy to overlook.
<?php
function mimic_array_sum($array) {
$sum = 0;
foreach($array as $number) {
$sum = $sum + $number;
}
return $sum;
}
?>
Hope this helps.
Christopher Hall
9,052 PointsRobert Walker's answer is correct, but it's good to note that $number
here is a local variable to the foreach
function. You would probably get an error if you tried to return it from the mimic_array_sum
function since it doesn't exist outside of the foreach
.
Andrew Shook
31,709 PointsAndrew Shook
31,709 PointsCould you post some code or elaborate on your problem a little bit.