1. JavaScript Program to Swap the Values of Two Variables Using a Third Variable
Swapping the values of two variables is a basic operation in many programming tasks. In JavaScript, you can achieve this using a third variable.
JavaScript Code:
// Function to swap the values of two variables
function swapValues(a, b) {
let temp = a;
a = b;
b = temp;
return { a: a, b: b };
}
// Example usage
let x = 5;
let y = 10;
let swapped = swapValues(x, y);
console.log(`After swapping: x = ${swapped.a}, y = ${swapped.b}`);
In this program, we define a function swapValues() that swaps the values of two variables a and b using a third variable temp to temporarily hold one of the values. For example, if x = 5 and y = 10, after swapping, x will be 10, and y will be 5.
2. JavaScript Program to Convert Celsius to Fahrenheit
Temperature conversion is an essential task in many applications. Here, we will learn how to convert a temperature from Celsius to Fahrenheit using JavaScript.
Formula:
The formula for converting Celsius to Fahrenheit is:
Where:
C is the temperature in CelsiusF is the temperature in Fahrenheit
JavaScript Code:
// Function to convert Celsius to Fahrenheit
function celcToFahr(celsius) {
return (celsius * 9/5) + 32;
}
// Example usage
let celsius = 20;
let fahrenheit = celcToFahr(celsius);
console.log(`${celsius} Celsius is equal to ${fahrenheit} Fahrenheit.`);
This function accepts a Celsius value and returns the corresponding Fahrenheit temperature. For example, if you input 20°C, the output will be 68°F.
3. JavaScript Program to Find Area and Perimeter of a Rectangle
In geometry, working with basic shapes like rectangles is common. JavaScript can help you calculate the area and perimeter of a rectangle with just a few lines of code.
Formulas:
- Area:
A = \text{length} \times \text{width} - Perimeter:
P = 2 \times (\text{length} + \text{width})
JavaScript Code:
// Function to calculate area and perimeter of a rectangle
function calc(len, wid) {
let area = len * wid;
let perimeter = 2 * (len + wid);
return { area: area, perimeter: perimeter };
}
// Example usage
let length = 10;
let width = 5;
let result = calc(length, width);
console.log(`Area: ${result.area}, Perimeter: ${result.perimeter}`);
This program calculates both the area and perimeter of a rectangle given its length and width. For example, if the length is 10 units and the width is 5 units, the output will be an area of 50 and a perimeter of 30.
4. JavaScript Program to Find Simple Interest
Simple interest is a common financial calculation. It’s used in various scenarios, such as calculating the interest on loans or investments. The formula for calculating simple interest is:
Formula:
Where:
P is the principal amount (initial investment or loan)T is the time period (in years)R is the rate of interest
JavaScript Code:
// Function to calculate simple interest
function calculateSimpleInterest(principal, time, rate) {
return (principal * time * rate) / 100;
}
// Example usage
let principal = 10000;
let time = 5; // in years
let rate = 5; // in percentage
let interest = calculateSimpleInterest(principal, time, rate);
console.log(`The simple interest is ${interest}`);
This program calculates simple interest based on the principal, time, and interest rate. For example, if you have a principal of 10,000, a rate of 5%, and a time period of 5 years, the simple interest will be 2500.