Skip to content
Advertisement

I want Fibonacci string as concat string with previous 2 array element [closed]

    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"]);
?>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement