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
omid nassir
8,771 PointsCode Challenge Mimic array sum function
Hi, im stuck at this code challenge and i don't know how to fix it:
It says to "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."
Bummer! The sum of the three numbers in the array should be 17129. (If you need assistance with the code inside the function, check out the starting code for the previous Introducing Functions challenge.)
<?php function mimic_array_sum($array) { $count = 0; foreach($array as $element) { $count = $count + 1; } return count; }
$palindromic_primes = array(11, 757, 16361); $count = mimic_array_sum($palindromic_primes); echo $count; ?>
9 Answers
Miles Annon
2,072 PointsHi Omid, The question only asks you to create the function including the variable and return value. As we are mimicking the array_sum, your variables will need to use the array_sum function as per the code below. The count variable Randy used was to mimic the count function.
<?php function mimic_array_sum($array) { foreach($array as $element) { $sum = array_sum($array); } return $sum; } $palindromic_primes = array(11, 757, 16361); ?>
Hope this helps
CJ Williams
34,372 PointsI agree with you Robert. The majority of this php course has been very confusing and it seems like the things discussed in the video lessons give no mention to what is actually performed in the code challenges.
Nic Henstridge
2,107 PointsIn agreement with you CJ Williams, it isn't taught particularly well to really cover the material properly.
Ralph Maines
7,722 PointsOmid, I messed up. There is nothing wrong with the question and you don't have to use the array_sum function. Instead of doing $count = $count + 1, use $count = $count + $element because we are not adding the number of elements in the array but rather the sum of the elements. Whew, I think looking at the screen too long plays games with my mind. I know your question is from a long time ago, but I thought you might want to know what was going wrong.
Jamal Scott
9,656 PointsYour block of code made sense when i read it so i tried it and it worked. It should look like this: function mimic_array_sum($array) { foreach($array as $element) { $sum = $sum + $element; } return $sum; }. For the other people who are using the array_sum php function; you're doing the code challenge wrong. You're supposed to recreate that function to display the same output that it would if you did use it. So in other words create your own function that does the same thing as the array_sum php function. I know this course is a bit confusing at times but if your stuck then try to read through the answer to see if u make sense of it = <?php
function mimic_array_sum($array){ $sum = 0; foreach($array as $count){ $sum = $sum + $count;
} return $sum; }
$palindromic_primes = array(11, 757, 16361); $sum = mimic_array_sum($palindromic_primes); echo $sum;
?>
Kristi Smythe
10,665 PointsI think the testing and troubleshooting value of these challenges is worth the effort, particularly from the perspective of engaging our creative problem solving and browser error investigation skills.
I wrote the mimic_count example code in my test.php file based on the video example for comparison. Then added the given code for this mimic_array_sum challenge, and began experimenting. The errors I got in my browser helped me sort out how and where to fix. This is what I came up with to get the sum after troubleshooting:
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($palindromic_primes);
echo $sum;
ELIZA D
Courses Plus Student 276 Pointsim stuck here , too ....
<?php function mimic_array_sum($array) { $sum = $sum + $element foreach($array as $element) { $sum = array_sum($array); } return $sum; } $palindromic_primes = array(11, 757, 16361); ?>
Ralph Maines
7,722 PointsYou have some extra code in there that you don't need and some more in the wrong place. The $sum=$sum+$element; should be inside the foreach loop because this is where "$element" is created. Don't forget the colon at the end. The $sum=array_sum($array); should be deleted because it accomplishes the same thing as $sum=array_sum($array);. The difference is that the $sum=array_sum($array); is a function that already exists in php and the $sum=$sum+$element is our recreation of it. It should look like this: function mimic_array_sum($array) { foreach($array as $element) { $sum = $sum + $element; } return $sum; }
Daniel Brown
16,908 PointsI am also totally lost on this question?
Jamal Scott
9,656 Pointsyou're supposed to code your own version of the array_sum php function, so in other words create a function called "mimic_array_sum" that does the exact same thing that the "array_sum" function should do.
raydelprado
10,350 Points<code> function mimic_array_sum($array){ $sum = 0; foreach($array as $element){ $sum = $sum + $element; } return $sum; } </code>
omid nassir
8,771 Pointsomid nassir
8,771 PointsOkay thank you. I was able to figure it out.
Robert Bullen
8,022 PointsRobert Bullen
8,022 PointsIsn't that defeating the purpose of the exercise? to use the function you're suppose to be replicating