-
-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f26e2d
commit 42569eb
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
packages/kitchen-sink/src/examples/investment-calculator.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { useState } from 'react'; | ||
import { block } from 'million/react'; | ||
|
||
interface Results { | ||
totalInvestment: number; | ||
totalPayment: number; | ||
totalInterest: number; | ||
} | ||
|
||
const InvestmentCalculator = block(() => { | ||
const [investmentAmount, setInvestmentAmount] = useState<number | string>(''); | ||
const [interestRate, setInterestRate] = useState<number | string>(''); | ||
const [numberOfYears, setNumberOfYears] = useState<number | string>(''); | ||
const [results, setResults] = useState<Results | null>(null); | ||
|
||
|
||
const calculateInterest = ()=>{ | ||
const p = parseFloat(investmentAmount as string); | ||
const r = parseFloat(interestRate as string); | ||
const n = parseInt(numberOfYears as string, 10); | ||
|
||
if (isNaN(p) || isNaN(r) || isNaN(n) || p <= 0 || r <= 0 || n <= 0) { | ||
alert('Please enter valid numbers.'); | ||
return; | ||
} | ||
|
||
const totalInterest = (p*r*n)/100; | ||
const totalPayment = totalInterest + p; | ||
const totalInvestment = p; | ||
|
||
setResults({ | ||
totalInvestment, | ||
totalPayment, | ||
totalInterest, | ||
}); | ||
} | ||
|
||
|
||
return ( | ||
<div className="InvestmentCalculator"> | ||
<h1>Investment Calculator</h1> | ||
<div> | ||
<label>Total Investment ($): </label> | ||
<input | ||
type="number" | ||
value={investmentAmount} | ||
onChange={(e) => setInvestmentAmount(e.target.value)} | ||
style={{ display: 'block', marginBottom: '15px' }} | ||
/> | ||
</div> | ||
<div> | ||
<label>Interest Rate (Annual %): </label> | ||
<input | ||
type="number" | ||
value={interestRate} | ||
onChange={(e) => setInterestRate(e.target.value)} | ||
style={{ display: 'block', marginBottom: '15px' }} | ||
/> | ||
</div> | ||
<div> | ||
<label>Duration (Years): </label> | ||
<input | ||
type="number" | ||
value={numberOfYears} | ||
onChange={(e) => setNumberOfYears(e.target.value)} | ||
style={{ display: 'block', marginBottom: '15px' }} | ||
/> | ||
</div> | ||
<button onClick={calculateInterest}>Calculate</button> | ||
|
||
{results && ( | ||
<div className="results"> | ||
<h2>Results:</h2> | ||
<div>End Balance: ${results.totalPayment.toFixed(2)}</div> | ||
<div>Total Investment : ${results.totalInvestment.toFixed(2)}</div> | ||
<div>Total Interest: ${results.totalInterest.toFixed(2)}</div> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}); | ||
|
||
export default InvestmentCalculator; |
42569eb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
million-kitchen-sink – ./packages/kitchen-sink
million-kitchen-sink.vercel.app
million-kitchen-sink-git-main-millionjs.vercel.app
million-kitchen-sink-millionjs.vercel.app
42569eb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
sink – ./packages/kitchen-sink
sink-git-main-millionjs.vercel.app
million-kitchen-sink-atit.vercel.app
sink.million.dev
sink-millionjs.vercel.app