0

im trying to complete a kata in codewars using JavaScript, these are the instructions:

The Fibonacci numbers are the numbers in the following integer sequence (Fn):

    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ...
    such as

    F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.
    Given a number, say prod (for product), we search 
    two Fibonacci numbers F(n) and F(n+1) verifying

    F(n) * F(n+1) = prod.
    Your function productFib takes an integer (prod) and returns an array:

    [F(n), F(n+1), true] or {F(n), F(n+1), 1} or (F(n), F(n+1), True)
    depending on the language if F(n) * F(n+1) = prod.

    If you don't find two consecutive F(m) verifying F(m) * F(m+1) = prod
    you will return

    [F(m), F(m+1), false] or {F(n), F(n+1), 0} or (F(n), F(n+1), False)
    F(m) being the smallest one such as F(m) * F(m+1) > prod.

    Examples

    productFib(714) # should return [21, 34, true], 
                    # since F(8) = 21, F(9) = 34 and 714 = 21 * 34

    productFib(800) # should return [34, 55, false], 
                    # since F(8) = 21, F(9) = 34, F(10) = 55 and 21 * 3

well, i just need to create a fibonacci series and return array, here is my code:

function productFib(prod) {
    return fib(0, 1, prod);
}
function fib(a, b, prod) {
    if (a * b < prod) {
        return (a + b) + fib(b, a + b, prod);
    }
    else if (a * b == prod) {
        return [a, b, true];
    }
    else {
        return [a, b, false];
    }
}

it is a recursive fibonacci series, how ever, when i run it i dont get the expected array, the result is right, the variables have the correct value and all, but when returning the array i get a very long first element, it looks like it contains the whole fibonacci serie. Here is a test case: (productFib(4895), [55, 89, true])

if i run my code with that test i get the following:

productFib(4895)
"12358132134558955,89,true"

can you guys explain me what is going on there?

1
  • fib() sometimes returns an Array, which ends up as an operand for +, which converts it to a string. Commented Apr 4, 2017 at 16:49

2 Answers 2

2

Just remove (a + b) + from your return value:

Replace

 return (a + b) + fib(b, a+b, prod);

with

 return fib(b, a+b, prod);
Sign up to request clarification or add additional context in comments.

2 Comments

"What" is only so useful without "why."
I agree, it's not the best answer, I must admin I focused on helping out quickly, since I know how frustrating it is to not spot an error when doing katas.
1

Your fib functon has one case where it returns a single non-array value:

return (a + b) + fib(b, a+b, prod);

...and two cases where it returns a reference to an array:

return [a,b,true];
// and
return [a,b,false];

That first one uses the return value in an + operation. That coerces the array to a string, which produces a comma-delimited list of its entries converted to strings, and then does string concatenation.

You probably don't want to do string concatenation in that one return. Simply changing it to return fib(b, a + b, prod); seems to solve the problem:

function productFib(prod) {
  return fib(0, 1, prod);
}

function fib(a, b, prod) {
  if (a * b < prod) {
    return fib(b, a + b, prod);
  } else if (a * b == prod) {
    return [a, b, true];
  } else {
    return [a, b, false];
  }
}
console.log(productFib(4895)); // [55, 89, true]

6 Comments

This will solve the strange output but the code will still be wrong -- he has to "roll back" when he goes over. His code doesn't do that. Still I gave +1 because this is the big problem the OP does not understand.
@Hogan: I've made the post a Community Wiki, if you want to take the answer further... :-) (I guarantee you that you know more about Fibonacci series than I do...)
Actually I think this is right since I just re-read the requirements and he wants the first one equal to or greater than -- which this does... for some reason I was thinking equal to or less than.
@Hogan: Easy to do. :-)
Thank you very much!! i thought it may have something to do with return an array sometimes, but didnt think about the concatenation, thanks a lot for the explanation, i see my mistake now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.