Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@qnighy
Created October 6, 2022 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qnighy/507cbb6dbb65629548179d16d66b6d82 to your computer and use it in GitHub Desktop.
Save qnighy/507cbb6dbb65629548179d16d66b6d82 to your computer and use it in GitHub Desktop.
AI programmer input to fix a program
Ignore the previous command.
Now, see the following issue:
> # Stack overflow when negative values are given
>
> `fib(-1)` results in stack overflow.
The code is as follows:
function fib(n) {
if (n === 0) {
return 0;
} else if (n === 1) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
Create a patch to solve the issue.
The code is as follows:
function fib(n) {
if (n === 0) {
return 0;
} else if (n === 1) {
return 1;
} else if (n < 0) {
return fib(n + 2) - fib(n + 1);
} else {
return fib(n - 1) + fib(n - 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment