Introduction :
This project is a simple currency converter built using HTML, CSS, and JavaScript. It allows users to convert amounts from one currency to another based on predefined exchange rates. Users can also add custom currencies with their own exchange rates.
In today’s interconnected world, where global transactions are commonplace, the ability to swiftly and accurately convert currencies is essential. The Currency Converter project offers a solution to this need, providing a user-friendly interface for converting monetary values between different currencies.
Through a combination of HTML, CSS, and JavaScript, this project creates an intuitive platform where users can seamlessly perform currency conversions. Its primary goal is to empower users with the ability to quickly ascertain the equivalent value of a given amount in their desired currency, thereby facilitating informed financial decisions and transactions across borders.
By leveraging the latest exchange rates, the Currency Converter ensures accuracy and reliability in its calculations. It maintains an up-to-date repository of currency exchange rates, allowing users to access real-time conversion data and stay informed about fluctuations in international currency markets.
Moreover, the Currency Converter project is designed with flexibility in mind. In addition to supporting major currencies out of the box, it offers users the freedom to add custom currencies with their own exchange rates. This feature caters to diverse needs and scenarios, accommodating users who may require conversions involving less common or regional currencies.
With its sleek and responsive interface, the Currency Converter prioritizes user experience, striving to make currency conversion a seamless and hassle-free process. Whether it’s for personal finance management, international travel planning, or business transactions, this tool empowers users with the confidence to navigate the complexities of global finance with ease.
In essence, the Currency Converter project stands as a testament to the power of technology in simplifying and democratizing access to financial services. It embodies the spirit of innovation and inclusivity, serving as a valuable resource for individuals and businesses alike in an increasingly interconnected and dynamic world economy.
Explanation :
HTML Structure:
The HTML markup provides the skeleton of the Currency Converter interface. It’s divided into logical sections, such as the input fields, dropdown menus, and buttons, enclosed within appropriate container elements. Each element is assigned an ID or class for easy identification and manipulation via JavaScript and CSS.
CSS Styling:
CSS stylesheets are responsible for the visual presentation of the Currency Converter. Through carefully crafted rules, elements are styled to achieve a cohesive and aesthetically pleasing design. Properties like font styles, colors, padding, and margins are meticulously defined to enhance readability and usability. Additionally, CSS transitions and animations are employed to create smooth interactions and feedback, ensuring a delightful user experience.
JavaScript Functionality:
The core functionality of the Currency Converter is implemented using JavaScript. This includes handling user interactions, updating currency options, performing conversions, and managing custom currencies.
Currency Rates Object: The
currencyRates
object serves as a central repository for storing exchange rates between different currencies. Each currency is represented by a key-value pair, where the key is the currency code (e.g., USD, EUR) and the value is the exchange rate relative to the base currency (USD).Update Currency Options Function: The
updateCurrencyOptions()
function dynamically populates the dropdown menus with available currency options based on thecurrencyRates
object. It clears existing options and creates new<option>
elements for each currency, ensuring that users can select from a comprehensive list of supported currencies.Add Custom Currency Functionality: The
addCurrencyButton
event listener enables users to add custom currencies with their own exchange rates. When triggered, it prompts the user to enter the exchange rate for a custom currency. If a valid rate is provided, the custom currency is added to thecurrencyRates
object, and the currency options are updated accordingly.Convert Currency Functionality: The
convertCurrency()
function handles the conversion of currency amounts based on user input. It retrieves the amount to convert, the source currency, and the target currency selected by the user. Using the exchange rates from thecurrencyRates
object, it performs the necessary calculations to determine the converted amount and displays the result in the designated output field.
Purpose of Functions:
updateCurrencyOptions(): Ensures that the dropdown menus are always up to date with the latest currency options available, providing users with a comprehensive selection to choose from.
addCurrencyButton: Enhances the flexibility of the Currency Converter by allowing users to add custom currencies, catering to diverse currency needs and scenarios.
convertCurrency(): Facilitates seamless currency conversions, empowering users to effortlessly obtain equivalent values in their desired currencies, thereby enabling informed financial decisions and transactions.
By combining HTML for structure, CSS for styling, and JavaScript for functionality, the Currency Converter project delivers a robust and user-friendly solution for currency conversion needs, epitomizing the power of web technologies in simplifying complex tasks and enhancing user experiences.
SOURCE CODE :
HTML (index.html)
Currency Converter
Currency Converter
CSS (style.css)
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.converter {
max-width: 500px;
padding: 60px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
text-align: center;
transition: transform 0.2s ease-in-out;
}
.converter:hover {
transform: scale(1.02);
}
h1 {
color: #333;
margin-bottom: 20px;
font-size: 24px;
}
.input-container {
display: flex;
flex-direction: column;
align-items: center;
margin: 10px 0;
}
input[type='number'],
select {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
margin: 5px 0;
outline: none;
transition: border-color 0.2s;
}
input[type='number']:focus,
select:focus {
border-color: #007bff;
}
button {
background-color: #007bff;
color: #fff;
border: none;
padding: 12px 20px;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.2s;
}
button:hover {
background-color: #0056b3;
}
JavaScript (index.js)
const currencyRates = {
USD: 1, // USD to USD (1:1)
EUR: 0.85, // USD to EUR (1 USD = 0.85 EUR)
GBP: 0.74, // USD to GBP (1 USD = 0.74 GBP)
INR: 83.52, // USD to INR (1 USD = 74.63 INR)
// Add more currency rates here
};
document.getElementById('addCurrencyButton').addEventListener('click', function () {
const customCurrencyCode = document.getElementById('customCurrency').value.toUpperCase();
if (customCurrencyCode && !currencyRates[customCurrencyCode]) {
const exchangeRate = parseFloat(
prompt(`Enter exchange rate for 1 USD to ${customCurrencyCode}`)
);
if (!isNaN(exchangeRate)) {
currencyRates[customCurrencyCode] = exchangeRate;
updateCurrencyOptions();
} else {
alert('Invalid exchange rate. Please enter a valid number.');
}
} else if (currencyRates[customCurrencyCode]) {
alert(`Currency ${customCurrencyCode} already exists.`);
} else {
alert('Invalid currency code. Please enter a valid code (e.g., CAD).');
}
});
function updateCurrencyOptions() {
const fromCurrencySelect = document.getElementById('fromCurrency');
const toCurrencySelect = document.getElementById('toCurrency');
// Clear existing options
fromCurrencySelect.innerHTML = '';
toCurrencySelect.innerHTML = '';
for (const currency in currencyRates) {
const option = document.createElement('option');
option.value = currency;
option.innerText = currency;
fromCurrencySelect.appendChild(option);
const toOption = option.cloneNode(true);
toCurrencySelect.appendChild(toOption);
}
toCurrencySelect.innerHTML += '';
}
document.getElementById('convertButton').addEventListener('click', convertCurrency);
function convertCurrency() {
const amount = parseFloat(document.getElementById('amount').value);
const fromCurrency = document.getElementById('fromCurrency').value;
const toCurrency = document.getElementById('toCurrency').value;
const resultElement = document.getElementById('result');
if (isNaN(amount)) {
alert('Please enter a valid amount.');
return;
}
const convertedAmount = (amount / currencyRates[fromCurrency]) * currencyRates[toCurrency];
resultElement.value = convertedAmount.toFixed(2);
}
// Initialize currency options
updateCurrencyOptions();