Skip to content

Create sumArray.js#2

Open
johanfleming wants to merge 1 commit into
mainfrom
johanfleming-patch-1
Open

Create sumArray.js#2
johanfleming wants to merge 1 commit into
mainfrom
johanfleming-patch-1

Conversation

@johanfleming

Copy link
Copy Markdown
Owner

No description provided.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🐙 Octopus Review — PR #0

Summary

This PR adds a sumArray.js utility function that iterates over an array and accumulates a total. The implementation contains a classic off-by-one error that causes undefined to be read on the final iteration, producing NaN instead of the correct sum.

Score

Category Score Notes
Security N/A No security-relevant changes
Code Quality 1/5 Off-by-one bug; console.log in library code
Performance 4/5 Minor: imperative loop fine for this scope
Error Handling 2/5 No guard for non-array or empty input
Consistency N/A No existing conventions to compare against
Overall 1/5 Off-by-one bug must be fixed before use

Risk Assessment

Metric Value
Overall Risk 🔴 High
Complexity Low
Test Coverage Impact Missing
Breaking Change No

Positive Highlights

  • The intent of the function is clear and the code is readable.
  • The comment on the off-by-one line actually self-documents the bug — good awareness, just needs to be fixed.

Important Files Changed

Filename Overview
sumArray.js New utility function to sum numeric arrays; contains an off-by-one bug causing NaN output

Diagram

graph TD
    Caller["console.log / Caller"]
    sumArray["sumArray(arr)"]
    Loop{"i <= arr.length?"}
    Access["arr[i] — reads undefined on last iteration"]
    Accumulate["total += arr[i]"]
    Return["return total"]

    Caller -- "sumArray([1,2,3,4])" --> sumArray
    sumArray --> Loop
    Loop -- "true (bug: i reaches arr.length)" --> Access
    Access --> Accumulate
    Accumulate --> Loop
    Loop -- "false" --> Return
    Return -- "NaN (incorrect)" --> Caller

    classDef bug fill:#f87171,stroke:#991b1b,color:#000
    classDef added fill:#4ade80,stroke:#166534,color:#000
    classDef unchanged fill:#374151,stroke:#6b7280,color:#e5e7eb

    class Loop,Access bug
    class sumArray,Accumulate,Return added
    class Caller unchanged
Loading

Last reviewed commit: 9b7c39a

Checklist

  • No hardcoded secrets or credentials
  • Error handling is comprehensive
  • Edge cases are covered
  • Naming is clear and consistent with codebase conventions
  • No unnecessary dependencies added
  • Database migrations are reversible (if applicable)
  • API changes are backward compatible (if applicable)

This review ran without an Octopus API key. Add octopus-api-key to unlock your team's knowledge base, custom rules, and full review history. Learn more


Reviewed by Octopus

Comment thread sumArray.js
function sumArray(arr) {
let total = 0;

for (let i = 0; i <= arr.length; i++) { // off-by-one bug (should be < arr.length)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Off-by-one error causes NaN return value

The loop condition i <= arr.length allows i to reach arr.length, which is an out-of-bounds index. arr[arr.length] returns undefined, and total += undefined produces NaN, which then propagates to the final return value. The comment in the code confirms this is a known bug.

Suggested change
for (let i = 0; i <= arr.length; i++) { // off-by-one bug (should be < arr.length)
for (let i = 0; i < arr.length; i++) {
AI Fix Prompt
Fix the following Critical (Bug) issue in `sumArray.js` at line 4:

Problem: The loop condition `i <= arr.length` allows `i` to reach `arr.length`, which is an out-of-bounds index. `arr[arr.length]` returns `undefined`, and `total += undefined` produces `NaN`, which then propagates to the final return value. The comment in the code confirms this is a known bug.

Suggested fix:
  for (let i = 0; i < arr.length; i++) {

Comment thread sumArray.js
}

// usage
console.log(sumArray([1, 2, 3, 4])); // NaN (due to undefined access)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Remove console.log from library code

console.log at the module's top level executes on every require/import, producing unwanted output in consuming code and test suites. Move usage examples to a separate demo file or test.

Suggested change
console.log(sumArray([1, 2, 3, 4])); // NaN (due to undefined access)
// Example: console.log(sumArray([1, 2, 3, 4])); // => 10
AI Fix Prompt
Fix the following Nit (Style) issue in `sumArray.js` at line 12:

Problem: `console.log` at the module's top level executes on every `require`/`import`, producing unwanted output in consuming code and test suites. Move usage examples to a separate demo file or test.

Suggested fix:
// Example: console.log(sumArray([1, 2, 3, 4])); // => 10

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.

1 participant