I solve problems on the compilation of functions for beginners, which are checked on the expected input data with the expected results. In the expected result, the function should return an array of integers in the form [0, 32, 18] , and I can only do [0,32,18] . Here is the full function:

 function race($v1, $v2, $g) { if ($v1 >= $v2) { return null; } for ($i = 0; $i >= 0; $i++) { $tort1 = $v1 / 3600 * $i + $g; $tort2 = $v2 / 3600 * $i; if ($tort1 <= $tort2) { break; } } $time = $i - 1; $hours = floor($time / 3600); if ($hours < 1) { $minutes = floor($time / 60); } else { $minutes = floor(($time - $hours * 3600) / 60); } $seconds = $time - $hours * 3600 - $minutes * 60; $result = array($hours, $minutes, $seconds); $b = [$hours, $minutes, $seconds]; return $b; } 

Tell me, please, how to return it with spaces after a comma, taking into account that the result is an integer data array?

    4 answers 4

    It turns out that the problem was in the data type: I did not give all the results to integer , so the essence of the problem was interpreted incorrectly, I apologize.

      Spaces like ba, purely aesthetic to make it easy to read.

       $b = [$hours, $minutes, $seconds]; return $b; 

      you define an array and return it. All right it's the same as

       $array = array($hours, $minutes, $seconds); 
      • This is the test result: $ test-> describe ('Start tests', function () use ($ test) {$ test-> assert_equals (race (720, 850, 70), [0, 32, 18]);} With all this, my result [0,32,18] is not accepted. - Rubick
      • well, without seeing the code of the function itself that you call, I cannot answer you. Complete your question with more detailed sources. Ato, the answers will be like a pump into the sky. - m.sultan
      • Completed, now the whole function - Rubick

      array is a data structure. spaces that you want to see somewhere, do not belong to the structure itself. This is the output / submission form. Therefore, in general, the array itself does not contain any commas or spaces, or even these parentheses (since this is just a syntactic form of the record). But if you want to print this array, then in the form of a string, you are free to output it as you like, but it will already be a string, not an array.

       function array2str($data){ return "[".implode(', ', $data)."]"; } print array2str([0,38,12]); 
      • That is, outputting an array with integer elements through return is basically impossible in the form that is expected? - Rubick
      • @Rubick return prints nothing, it returns the result of the function. - teran
      • Yes, I meant to return - Rubick

      It is best to use assertTrue instead of assertTrue , and compare arrays with a user-defined function.

      The code was not tested, check for yourself:

        /** * Determine if two associative arrays are similar * * Both arrays must have the same indexes with identical values * without respect to key ordering * * @param array $a * @param array $b * @return bool */ function arrays_are_similar($a, $b) { // if the indexes don't match, return immediately if (count(array_diff_assoc($a, $b))) { return false; } // we know that the indexes, but maybe not values, match. // compare the values between the two arrays foreach($a as $k => $v) { if ($v !== $b[$k]) { return false; } } // we have identical indexes, and no unequal values return true; } 

      In the test:

       $this->assertTrue(arrays_are_similar($foo, $bar)); 
      • The check is framed by the creators of the tasks and the environment in which I compose a function, and I cannot change it - Rubick