Skip to content

Commit

Permalink
Clean up incorrectly parsed form data
Browse files Browse the repository at this point in the history
  • Loading branch information
Shpigford committed Jun 10, 2024
1 parent 4e309c5 commit ab8b8e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions app/javascript/controllers/loan_calculator_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export default class extends Controller {
calculate(event) {
event.preventDefault();
const formData = new FormData(event.target);
const loanAmount = parseFloat(formData.get("loan_amount"));
const interestRate = parseFloat(formData.get("interest_rate"));
const loanTerm = parseFloat(formData.get("loan_term"));
const loanPeriod = formData.get("loan_period") || "years"; // Default loanPeriod to "years"
const parseFormData = key => parseFloat(formData.get(key).replace(/[^0-9.-]+/g, ''));

const loanAmount = parseFormData("loan_amount");
const interestRate = parseFormData("interest_rate");
const loanTerm = parseFormData("loan_term");
const loanPeriod = parseFormData("loan_period") || "years"; // Default loanPeriod to "years"
const date = formData.get("date");

let numberOfPayments;
Expand Down
10 changes: 6 additions & 4 deletions app/javascript/controllers/roi_calculator_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export default class extends Controller {
calculate(event) {
event.preventDefault();
const formData = new FormData(event.target);
let amountInvested = parseFloat(formData.get("amount_invested"));
const amountReturned = parseFloat(formData.get("amount_returned"));
let investmentLength = parseFloat(formData.get("investment_length"));
const investmentPeriod = formData.get("investment_period");
const parseFormData = key => parseFloat(formData.get(key).replace(/[^0-9.-]+/g, ''));

const amountInvested = parseFormData("amount_invested");
const amountReturned = parseFormData("amount_returned");
const investmentLength = parseFormData("investment_length");
const investmentPeriod = parseFormData("investment_period");

// Convert investmentLength to years if needed
if (investmentPeriod === "weeks") {
Expand Down

0 comments on commit ab8b8e4

Please sign in to comment.