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


Introducing User-Defined Functions task 2 of 3 : Broken?

On Introducing User-Defined Functions task 2 of 3 I am trying to pass this code 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, and a return command to send the sum back to the main code."

Below is my code, I even tried another users 'verified' code in a previous post and while it passes for some it doesn't for others.

The error I get is

"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.)"

Is this a bug in the code challenge?

  function mimic_array_sum( $array ) {

    $count = 0;

    foreach ( $array as $arrays ) {
        $count = $count + $arrays;
      return $count;


  $palindromic_primes = array( 11, 757, 16361 );
  echo mimic_array_sum( $palindromatic_primes );

2 Answers

David Kaneshiro Jr.
David Kaneshiro Jr.
29,247 Points

Hi John,

You will need to move the return $count; statement out of the foreach loop. Currently your function returns the value for $count right after adding the first item of the array to the variable. The value for $count should be returned after the foreach loop has ended. For an example of what the code should look like check out the following forum post made a few of days ago.