Skip to content

adding a new method#73

Open
vlad-ko wants to merge 2 commits intomainfrom
feature/adding-ai-features
Open

adding a new method#73
vlad-ko wants to merge 2 commits intomainfrom
feature/adding-ai-features

Conversation

@vlad-ko
Copy link
Owner

@vlad-ko vlad-ko commented Jun 9, 2025

No description provided.

@codecov
Copy link

codecov bot commented Jun 9, 2025

Codecov Report

❌ Patch coverage is 0% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.03%. Comparing base (0bcf170) to head (667fb53).

Files with missing lines Patch % Lines
resources/js/classes/calculator.js 0.00% 6 Missing ⚠️

❌ Your patch check has failed because the patch coverage (0.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff              @@
##               main      #73      +/-   ##
============================================
- Coverage     80.28%   78.03%   -2.26%     
  Complexity       74       74              
============================================
  Files            13       13              
  Lines           208      214       +6     
  Branches          0        2       +2     
============================================
  Hits            167      167              
- Misses           41       47       +6     
Flag Coverage Δ
controller 50.00% <ø> (ø)
javascript 72.54% <0.00%> (-9.68%) ⬇️
service 87.85% <ø> (ø)
unit 71.05% <ø> (ø)
Components Coverage Δ
backend 79.75% <ø> (ø)
Files with missing lines Coverage Δ
resources/js/classes/calculator.js 57.89% <0.00%> (-26.73%) ⬇️
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vlad-ko
Copy link
Owner Author

vlad-ko commented Jun 9, 2025

@codecov-ai-reviewer test

@codecov-ai
Copy link
Contributor

codecov-ai bot commented Jun 9, 2025

On it! Codecov is generating unit tests for this PR.

@codecov-ai codecov-ai bot mentioned this pull request Jun 9, 2025
@vlad-ko
Copy link
Owner Author

vlad-ko commented Jun 9, 2025

@codecov-ai-reviewer review

@codecov-ai
Copy link
Contributor

codecov-ai bot commented Jun 9, 2025

On it! We are reviewing the PR and will provide feedback shortly.

@codecov-ai
Copy link
Contributor

codecov-ai bot commented Jun 9, 2025

PR Description

This pull request enhances the Calculator class by adding two new mathematical functions: absolute and factorial. The goal is to extend the calculator's functionality to support more complex calculations.

Click to see more

Key Technical Changes

The key technical changes involve the addition of the absolute(number) method, which returns the absolute value of a given number using Math.abs(), and the factorial(n) method, which calculates the factorial of a non-negative integer n using a recursive approach. The factorial method includes base cases to handle negative inputs (returning NaN) and inputs less than or equal to 1 (returning 1).

Architecture Decisions

The factorial function is implemented recursively. While this approach is concise and mirrors the mathematical definition, it may be subject to stack overflow issues for large input values. An iterative approach could be considered for improved robustness and performance with large numbers.

Dependencies and Interactions

This change only modifies the Calculator class and does not introduce any new dependencies. The new methods are self-contained and do not interact with other parts of the system beyond the potential for other modules to import and use the Calculator class.

Risk Considerations

The recursive implementation of the factorial function poses a risk of stack overflow for large input values. Additionally, there is no explicit input validation in the absolute method to ensure that the input is indeed a number. Reviewers should pay close attention to the potential for stack overflow in the factorial function and consider the need for input validation in the absolute function. The lack of unit tests for the new methods is also a risk.

Notable Implementation Details

The factorial function uses recursion, which might be less performant than an iterative solution for large numbers. The absolute function directly uses the built-in Math.abs() function. No specific error handling is implemented beyond returning NaN for negative inputs to the factorial function.

Comment on lines +31 to +35

factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;
return n * this.factorial(n - 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

The factorial method uses recursion which can lead to stack overflow for large inputs. Consider using an iterative implementation for better performance and to avoid potential stack overflow errors.

Suggested change
factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;
return n * this.factorial(n - 1);
factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}

Comment on lines +27 to +29

absolute(number) {
return Math.abs(number);
Copy link
Contributor

Choose a reason for hiding this comment

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

Input validation is missing in the absolute method. You should verify that the input is a valid number to prevent unexpected behavior.

Suggested change
absolute(number) {
return Math.abs(number);
absolute(number) {
if (typeof number !== 'number' || isNaN(number)) {
return NaN;
}
return Math.abs(number);
}

Comment on lines +31 to +35

factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;
return n * this.factorial(n - 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

The factorial function lacks a check for large inputs which could cause performance issues or stack overflow. Consider adding an upper limit.

Suggested change
factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;
return n * this.factorial(n - 1);
factorial(n) {
if (n < 0) return NaN;
if (!Number.isInteger(n)) return NaN;
if (n > 170) return Infinity; // JavaScript's maximum safe factorial
if (n <= 1) return 1;
return n * this.factorial(n - 1);
}

Comment on lines +27 to +35

absolute(number) {
return Math.abs(number);
}

factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;
return n * this.factorial(n - 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing JSDoc comments for the new methods. Adding documentation would improve code readability and maintainability.

Suggested change
absolute(number) {
return Math.abs(number);
}
factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;
return n * this.factorial(n - 1);
/**
* Returns the absolute value of a number
* @param {number} number - The input number
* @returns {number} The absolute value
*/
absolute(number) {
return Math.abs(number);
}
/**
* Calculates the factorial of a non-negative integer
* @param {number} n - The non-negative integer
* @returns {number} The factorial value, or NaN for invalid inputs
*/
factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;
return n * this.factorial(n - 1);
}

Comment on lines +27 to +35

absolute(number) {
return Math.abs(number);
}

factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;
return n * this.factorial(n - 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding unit tests for the new methods to ensure they work as expected with various inputs, including edge cases.

@vlad-ko
Copy link
Owner Author

vlad-ko commented Sep 26, 2025

@sentry review

Repository owner deleted a comment from seer-by-sentry bot Sep 26, 2025
@vlad-ko
Copy link
Owner Author

vlad-ko commented Sep 26, 2025

@sentry review

Comment on lines +28 to +30
absolute(number) {
return Math.abs(number);
}

Choose a reason for hiding this comment

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

Consider adding input validation to ensure the parameter is a number. The current implementation will pass through any input to Math.abs(), which may produce unexpected results for non-numeric inputs like strings or objects.

Suggested change
absolute(number) {
return Math.abs(number);
}
absolute(number) {
if (typeof number !== 'number' || isNaN(number)) {
throw new Error('Input must be a valid number');
}
return Math.abs(number);
}

Did we get this right? 👍 / 👎 to inform future reviews.

Comment on lines +32 to +36
factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;
return n * this.factorial(n - 1);
}

Choose a reason for hiding this comment

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

The recursive factorial implementation has potential performance and stack overflow issues for large values of n. Consider adding input validation for the upper bound and potentially implementing an iterative version for better performance and stack safety.

Suggested change
factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;
return n * this.factorial(n - 1);
}
factorial(n) {
if (typeof n !== 'number' || !Number.isInteger(n)) {
throw new Error('Input must be a valid integer');
}
if (n < 0) return NaN;
if (n > 170) throw new Error('Input too large, would cause overflow');
if (n <= 1) return 1;
// Iterative approach for better performance
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}

Did we get this right? 👍 / 👎 to inform future reviews.

Comment on lines +32 to +34
factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;

Choose a reason for hiding this comment

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

The factorial method should handle non-integer inputs more explicitly. Currently, it will attempt to calculate factorial for decimal numbers, which may not be the intended behavior. Consider checking if the input is an integer.

Suggested change
factorial(n) {
if (n < 0) return NaN;
if (n <= 1) return 1;
factorial(n) {
if (!Number.isInteger(n) || n < 0) return NaN;
if (n <= 1) return 1;
return n * this.factorial(n - 1);
}

Did we get this right? 👍 / 👎 to inform future reviews.

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