Title: Conditional statement after while loop working for most tests but not all (Fibonacci math logic problem)
Tags: javascript;while-loop;conditional-statements;fibonacci
Question: Working on a JavaScript problem regarding Fibonacci numbers from a codewars challenge here
Here is my code so far:
function productFib(prod){
let firstFib = 0;
let secondFib = 1;
while (prod &gt; firstFib * secondFib){
firstFib = firstFib + secondFib
secondFib = firstFib + secondFib
}
if (prod === firstFib*secondFib){
return [firstFib, secondFib, true]
} else {
firstFib = secondFib - firstFib;
secondFib = secondFib - firstFib;
return [firstFib, secondFib, false]
}
I have passed all tests except two: productFib(193864606) and productFib(602070). My results are [10946, 17711, false] and [610, 987, false] respectively, and they should say true instead of false. But if you multiply those Fibonacci numbers they equal the prod param, so I don't understand why my conditional statement isn't catching those two specifically.
Comment: *try this way insted* `function productFib(prod){
let num1 = 0,
num2 = 1;
while(num1 * num2 < prod){
num2 = num2 + num1
num1 = num2 - num1
return [num1, num2, num1 * num2 === prod]

Here is the accepted answer: Your logic needs tiny change. The way you are getting your pairs is :
function productFib(prod){
let firstFib = 0;
let secondFib = 1;
while (prod &gt; firstFib * secondFib){
firstFib = firstFib + secondFib
secondFib = firstFib + secondFib
console.log(firstFib,secondFib);
}
if (prod === firstFib*secondFib){
return [firstFib, secondFib, true]
} else {
firstFib = secondFib - firstFib;
secondFib = secondFib - firstFib;
return [firstFib, secondFib, false]
}
productFib(602070);
Your pairs are :
But you are missing the cases when 2,3 or 1,1 should be together.
That is because you are updating firstFib as firstFib + secondFib. Instead of that it should take the value of secondFib directly
function productFib(prod){
let firstFib = 0;
let secondFib = 1;
while (prod &gt; firstFib * secondFib){
let prevFirstFib = firstFib;
firstFib =  secondFib;
secondFib = prevFirstFib + secondFib;
console.log(firstFib, secondFib);
}
if (prod === firstFib*secondFib){
return [firstFib, secondFib, true]
} else {
firstFib = secondFib - firstFib;
secondFib = secondFib - firstFib;
return [firstFib, secondFib, false]
}
console.log(productFib(602070));
console.log(productFib(193864606));
Comment for this answer: Strange that this is marked as accepted, because it doesn't pass the tests on Code Wars.
Comment for this answer: Ahh that's right, the way I had it set up I was forgetting about 1,1 and 2,3. I fixed the while loop for assigning firstFib, and then removed the lines changing values of firstFib and secondFib in my else statement and now all my tests pass! Thanks so much!
Comment for this answer: Some `logs` go a long way in figuring out what is wrong.

Here is another answer: There are two issues:
The first loop jumps 2 Fibonacci steps in each iteration. But what was the greatest Fibonacci number of the two should remain, and be used as the least of the two.
The else part should not backtrack to a previous pair. The current pair is what you need to return. So you can do this without if..else even, and pass as boolean argument the equality check.
Correction:
function productFib(prod){
let firstFib = 0;
let secondFib = 1;
while (prod &gt; firstFib * secondFib){
[firstFib, secondFib] = [secondFib, firstFib + secondFib]
return [firstFib, secondFib, prod === firstFib*secondFib]
const assertSimilar = (a, b) =&gt; console.assert(JSON.stringify(a) === JSON.stringify(b));
assertSimilar(productFib(4895), [55, 89, true])
assertSimilar(productFib(5895), [89, 144, false])
assertSimilar(productFib(74049690), [6765, 10946, true])
assertSimilar(productFib(84049690), [10946, 17711, false])
assertSimilar(productFib(193864606), [10946, 17711, true])
assertSimilar(productFib(447577), [610, 987, false])
assertSimilar(productFib(602070), [610, 987, true])
console.log("tests passed");

Here is another answer: If you throw some logging statements in, it will be a lot easier to see what's going on. Your function actually fails with a much smaller number - 40. Putting in some logging, let's see what's happening:
function productFib(prod){
let firstFib = 0;
let secondFib = 1;
while (prod &gt; firstFib * secondFib){
console.log("before", firstFib, secondFib);
firstFib = firstFib + secondFib
secondFib = firstFib + secondFib
console.log("after", firstFib, secondFib);
console.log(prod, firstFib, secondFib)
if (prod === firstFib*secondFib){
return [firstFib, secondFib, true]
} else {
firstFib = secondFib - firstFib;
secondFib = secondFib - firstFib;
return [firstFib, secondFib, false]
console.log(productFib(40))
As you can see, it's skipping a number. Rather than using the previous secondFib as the new firstFib it's skipping it altogether, that's why your function is failing. A simple way to fix this is to save off the previous firstFib value, set firstFib = secondFib and then set secondFib = prevFirst + secondFib, like so:
function productFib(prod){
let firstFib = 0;
let secondFib = 1;
while (prod &gt; firstFib * secondFib){
console.log("before", firstFib, secondFib);
let prevFirst = firstFib;
firstFib = secondFib
secondFib = prevFirst + secondFib
console.log("after", firstFib, secondFib);
console.log(prod, firstFib, secondFib)
if (prod === firstFib*secondFib){
return [firstFib, secondFib, true]
} else {
firstFib = secondFib - firstFib;
secondFib = secondFib - firstFib;
return [firstFib, secondFib, false]
console.log(productFib(40))
Now you'll see that it's stepping through the numbers correctly and outputting what you expect!

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:02/16/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><while-loop><conditional-statements><fibonacci>', 'Title': 'Conditional statement after while loop working for most tests but not all (Fibonacci math logic problem)', 'ViewCount': 30, 'id': 71147654, 'user_AboutMe': '', 'user_AccountId': 20740326.0, 'user_DisplayName': 'J Bouche', 'user_Reputation': 3.0, 'user_Views': 3.0, 'user_id': 15230163.0}}