How to create NPV Calculator Card using JavaScript & Tailwind CSS ? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report A Net Present Value (NPV) Calculator is a web-based tool that helps users calculate the net present value of an investment. It typically includes input fields for the initial investment amount, the discount rate, and the expected cash flows. The calculator then calculates the NPV based on these inputs. The calculator is styled using Tailwind CSS, a utility-first CSS framework, which ensures a responsive and visually appealing design. Approach to Create NPV Calculator CardStart with the basic HTML structure, including the <!DOCTYPE html> declaration, <html>, <head>, and <body> tags. Import Tailwind CSS for styling.Create a container div with Tailwind CSS classes for styling. Inside the container, include elements for the app title, input fields for the initial investment, discount rate, and cash flows, buttons for calculating NPV and clearing inputs, and a div for displaying the result.Use JavaScript to handle the logic of calculating the NPV based on the initial investment, discount rate, and cash flows. Add event listeners to the calculate and clear buttons. When the calculate button is clicked, the script retrieves the values from the input fields, performs the calculation, and displays the result. When the clear button is clicked, the script clears the input fields and result.Use Tailwind CSS classes to style the app container, input fields, buttons, and result div. Customize the colors, fonts, and layout to create an appealing visual design for the NPV calculator.Ensure that the NPV calculator is responsive and works well on different screen sizes. Use Tailwind CSS classes for responsive design, such as sm:, md:, and lg: to adjust the layout and styling based on the screen size.Example: Implementation to design NPV Calculator HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>NPV Calculator</title> <script src="https://cdn.tailwindcss.com/3.4.16"></script> </head> <body class="bg-gray-100 flex justify-center items-center h-screen"> <div class="border-4 border-green-400 bg-white p-5 rounded-lg shadow-md w-full max-w-md"> <h1 class="text-3xl font-bold text-center text-green-600 mb-6"> NPV Calculator </h1> <div class="mb-4"> <label for="initialInvestment" class="block text-lg font-semibold text-gray-700"> Initial Investment ($) </label> <input type="number" id="initialInvestment" name="initialInvestment" placeholder="Enter initial investment" class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"> </div> <div class="mb-4"> <label for="discountRate" class="block text-lg font-semibold text-gray-700"> Discount Rate (%) </label> <input type="number" id="discountRate" name="discountRate" placeholder="Enter discount rate" class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"> </div> <div class="mb-4"> <label for="cashFlows" class="block text-lg font-semibold text-gray-700"> Cash Flows </label> <textarea id="cashFlows" name="cashFlows" rows="3" placeholder="Enter cash flows separated by commas" class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"> </textarea> </div> <div class="mb-4 flex justify-center"> <button onclick="calculateNPV()" class="px-6 py-3 bg-blue-500 text-white rounded-md hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50"> Calculate </button> <button onclick="clearInputs()" class="ml-4 px-6 py-3 bg-gray-300 text-gray-700 rounded-md hover:bg-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-300 focus:ring-opacity-50"> Clear </button> </div> <div id="result" class="text-xl font-semibold text-gray-800 text-center"></div> </div> <script> function calculateNPV() { const initialInvestment = document.getElementById('initialInvestment').value.trim(); const discountRate = document.getElementById('discountRate').value.trim(); const cashFlows = document.getElementById('cashFlows').value.trim(); if (!initialInvestment || !discountRate || !cashFlows) { alert('Please fill in all fields.'); return; } const initialInvestmentValue = parseFloat(initialInvestment); const discountRateValue = parseFloat(discountRate); const cashFlowsArray = cashFlows.split(',').map(flow => parseFloat(flow.trim())); let npv = -initialInvestmentValue; for (let i = 0; i < cashFlowsArray.length; i++) { npv += cashFlowsArray[i]/Math.pow(1 + discountRateValue / 100, i + 1); } document.getElementById('result').innerText = `NPV: $${npv.toFixed(2)}`; } function clearInputs() { document.getElementById('initialInvestment').value = ''; document.getElementById('discountRate').value = ''; document.getElementById('cashFlows').value = ''; document.getElementById('result').innerText = ''; } </script> </body> </html> Output: Comment More info M mguru4c05q Follow Improve Article Tags : JavaScript Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings6 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like