Crafting a Mutual Fund Returns Calculator as a Web Application: Step-by-Step Guide

Crafting a Mutual Fund Returns Calculator as a Web Application: Step-by-Step Guide

To build a Mutual Fund Returns Calculator with Next.js for the front end and Express.js for the back end, you can use the same steps we used for the compound interest calculator. Here's how:

Frontend (Next.js):

1. Create a new Next.js app:

npx create-next-app mutual-fund-calculator-frontend
cd mutual-fund-calculator-frontend

2. Create a mutual fund returns calculator component:

Create a new file called MutualFundCalculator.js inside the components directory:

// components/MutualFundCalculator.js
import { useState } from 'react';

const MutualFundCalculator = () => {
  const [investment, setInvestment] = useState('');
  const [rateOfReturn, setRateOfReturn] = useState('');
  const [years, setYears] = useState('');
  const [result, setResult] = useState('');

  const calculateReturns = () => {
    const i = parseFloat(investment);
    const r = parseFloat(rateOfReturn) / 100;
    const y = parseInt(years);

    const amount = i * Math.pow(1 + r, y);
    const returns = amount - i;
    setResult(returns.toFixed(2));
  };

  return (
    <div>
      <div>
        <label>Initial investment:</label>
        <input type="number" value={investment} onChange={(e) => setInvestment(e.target.value)} />
      </div>
      <div>
        <label>Annual rate of return (%):</label>
        <input type="number" value={rateOfReturn} onChange={(e) => setRateOfReturn(e.target.value)} />
      </div>
      <div>
        <label>Number of years:</label>
        <input type="number" value={years} onChange={(e) => setYears(e.target.value)} />
      </div>
      <button onClick={calculateReturns}>Calculate</button>
      {result && <p>Projected returns: ${result}</p>}
    </div>
  );
};

export default MutualFundCalculator;

3. Update the index.js page to use the MutualFundCalculator component:

Replace the content of pages/index.js with:

// pages/index.js
import MutualFundCalculator from '../components/MutualFundCalculator';

export default function Home() {
  return (
    <div>
      <h1>Mutual Fund Returns Calculator</h1>
      <MutualFundCalculator />
    </div>
  );
}

Backend (Express.js):

1. Create a new Express.js app:

Create a new directory for your backend, then navigate into it and initialize a new npm project:

mkdir mutual-fund-calculator-backend
cd mutual-fund-calculator-backend
npm init -y

2. Install Express.js:

npm install express

3. Create an Express server:

Create a file called server.js:

// server.js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 5000;

app.use(express.json());

app.post('/calculateMutualFundReturns', (req, res) => {
  try {
    const { investment, rateOfReturn, years } = req.body;
    const i = parseFloat(investment);
    const r = parseFloat(rateOfReturn) / 100;
    const y = parseInt(years);

    const amount = i * Math.pow(1 + r, y);
    const returns = amount - i;
    res.json({ returns: returns.toFixed(2) });
  } catch (error) {
    res.status(400).json({ error: 'Invalid input' });
  }
});

app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

4. Run the Express server:

node server.js

Testing:

Now, start your Next.js app (npm run dev) and go to http://localhost:3000. You'll see the interface for a mutual fund returns calculator. Type in your initial investment, the yearly rate of return, and how many years. After you click the "Calculate" button, you'll see the estimated returns under the calculator.

Make sure your Express server is running in a different terminal window. After you click the "Calculate" button, the front end sends a POST request to the Express server to figure out the expected returns from the information you entered. The server then sends back the calculated returns, and they show up on the front end.

Did you find this article valuable?

Support LingarajTechhub All About Programming by becoming a sponsor. Any amount is appreciated!