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?
fib()sometimes returns an Array, which ends up as an operand for+, which converts it to a string.