Skip to content

foundations/object_basics/03_fibonacci/solution/fibonacci-solution.js: Fix solution error handling for fibonacci() when count is NaN - #677

Open
Vairiation wants to merge 13 commits into
TheOdinProject:mainfrom
Vairiation:patch-1
Open

Vairiation wants to merge 13 commits into
TheOdinProject:mainfrom
Vairiation:patch-1

Conversation

@Vairiation

Copy link
Copy Markdown

Because

Previous logic for solution allows fibonacci('hello') to return 1 instead of 'OOPS'. While this is not a breaking change, I felt as though it was relevant when looking at the conditional logic.

This PR

  • Add !count to if conditional block that returns "OOPS" so it now reads if (!count || count < 0) return "OOPS"
  • Move if (count == 0) return 0 before if (!count || count < 0) return "OOPS" to allow proper handling of count = 0

Issue

Closes #XXXXX

Additional Information

Pull Request Requirements

  • [ x] I have thoroughly read and understand The Odin Project Contributing Guide
  • [ x] The title of this PR follows the location of change: brief description of change format, e.g. 01_helloWorld: Update test cases
  • [ x] The Because section summarizes the reason for this PR
  • [ x] The This PR section has a bullet point list describing the changes in this PR
  • [ x] If this PR addresses an open issue, it is linked in the Issue section
  • [ x] If this PR includes any changes that affect the solution of an exercise, I've also updated the solution in the /solutions folder

Previous logic allows fibonacci('hello') to return 1 instead of 'OOPS'
@Vairiation Vairiation closed this Sep 13, 2026
@Vairiation
Vairiation deleted the patch-1 branch September 13, 2026 03:47
@Vairiation
Vairiation restored the patch-1 branch September 13, 2026 03:49
@Vairiation Vairiation reopened this Sep 13, 2026

@mao-sz mao-sz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for suggesting this. I'm not actually convinced by it though. The point of the exercise isn't to make a robust function for real world usage that covers most/all possible cases, but rather to just practise some fundamentals in order to pass all the tests, for which NaN handling isn't.

That being said, looking at the current tests, I'm not a fan of the last few that deal with strings. Not really seeing the point having them in this exercise specifically; there are plenty of opportunities for parsing/converting strings to numbers throughout the whole curriculum.


I'd like to propose a different and slightly larger change to this exercise. If you're happy to do it, then just do the work in this branch. Otherwise, you can close the PR and I'll get the work handled another way.

I think instead of testing for string->number conversion, we should amend the instructions and tests so that if the argument is a negative number, NaN or not a number-type at all, return "OOPS", i.e. only proceed with positive numbers.

Means it focuses more on doing the fibonacci stuff and just shoving aside all other types of values, no other type-specific behaviour.

Update README with additonal input validation for the Fibonacci function.
Add additional tests to reject invalid inputs (ie. Boolean, NaN, non-number types, etc.)
…arguments

Refactor input validation to handle non-numeric and negative values. Simplify the logic for returning the Fibonacci result.
Updated student tests to ensure fibonacci function rejects invalid inputs following solution revision.
@Vairiation

Copy link
Copy Markdown
Author

Hey mao-sz, I just made the requested changes to the solution function, solution test suite, and student test suite.

I added in a check for NaN specifically since its type is a number and could slip through checking typeof countArg !== 'number' alone.

Let me know if that was the direction you were looking for. Also, this is my first pull request on a non-individual repo so let me know if I did something incorrectly.

@Vairiation
Vairiation requested a review from mao-sz September 14, 2026 22:33
Comment thread foundations/object_basics/03_fibonacci/solution/fibonacci-solution.js Outdated
Comment thread foundations/object_basics/03_fibonacci/solution/fibonacci-solution.js Outdated
Comment thread foundations/object_basics/03_fibonacci/solution/fibonacci-solution.js Outdated
Comment thread foundations/object_basics/03_fibonacci/solution/fibonacci-solution.js Outdated
Comment thread foundations/object_basics/03_fibonacci/fibonacci.spec.js Outdated
Comment thread foundations/object_basics/03_fibonacci/README.md Outdated
Comment thread foundations/object_basics/03_fibonacci/README.md Outdated
Comment thread foundations/object_basics/03_fibonacci/README.md Outdated
Vairiation and others added 8 commits September 16, 2026 00:17
…tion.js

Co-authored-by: mao-sz <122839503+mao-sz@users.noreply.github.com>
…tion.js

Co-authored-by: mao-sz <122839503+mao-sz@users.noreply.github.com>
…tion.js

Co-authored-by: mao-sz <122839503+mao-sz@users.noreply.github.com>
Co-authored-by: mao-sz <122839503+mao-sz@users.noreply.github.com>
Co-authored-by: mao-sz <122839503+mao-sz@users.noreply.github.com>
Co-authored-by: mao-sz <122839503+mao-sz@users.noreply.github.com>
@Vairiation

Copy link
Copy Markdown
Author

I think that should be everything from your suggestions. Let me know if I missed anything!

@Vairiation
Vairiation requested a review from mao-sz September 16, 2026 05:35

@mao-sz mao-sz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test changes need to be duplicated across the solution test file as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants