0

While working on a kata for Codewars, I am coming across an error when printing a value. Although the output of the code looks fine, I am getting the error that Array (...) does not match expected type "NULL".

Any ideas what I can change in my code to return a string?

function reverseSeq ($n) {
  $n = range($n, 1);
  foreach($n as $i) {
    if ($i > 1) {
      print_r ($i.",");
      $i--;
    } else {
      print_r ($i);
    }
  }
}

The output, when $n is 3, prints 3,2,1 as it should. However, it should return as a string rather than an integer.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

1 Answers1

2

For Codewars katas, you need to return the value to the testing function, which will then validate it. Printing to stdout is helpful for debugging, but has no bearing on the test's pass/failure outcome. Array (...) does not match expected type "NULL". is a generic PHPUnit failed assertion message that happens to not be especially helpful: it doesn't show the contents of the expected array or log the actual and expected values in the correct order. The author of the kata reversed the arguments to $this->assertEquals(reverseSeq(5), [5,4,3,2,1]);, so Array (...) is the value the test suite expects and NULL is what your function returns).

Try simply:

function reverseSeq($n) {
    return range($n, 1);
}

Outcome from the test runner:

You have passed all of the tests! :)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Thank you for the explanation. I'm a new coder as I am switching from network administration to programming. It seems I was overthinking this problem. – Nicolas Borysewich Apr 18 '19 at 23:25
  • No problem. Codewars is a great way to get started with some fun exercises once you get the hang of it. – ggorlen Apr 19 '19 at 00:33