fibStr(3, ["j", "h"]) ➞ "j, h, hj" fibStr(5, ["e", "a"]) ➞ "e, a, ae, aea, aeaae" fibStr(6, ["n", "k"]) ➞ "n, k, kn, knk, knkkn, knkknknk"
I just want a function that returns a response like : “n, k, kn, knk, knkkn, knkknknk”
Advertisement
Answer
Please try below code will help you:
<?php function fibStr($n, $arr) { $resp = $arr; for($ij= 2;$ij<$n;$ij++) { $resp[$ij] = $resp[$ij-1].$resp[$ij-2]; } return implode(", ",$resp); } echo fibStr(6, ["n", "k"]); ?>