0% found this document useful (0 votes)
3 views2 pages

JavaScript Calculator Functions

The document contains JavaScript code for a simple calculator application that allows users to perform basic arithmetic operations, power calculations, square roots, and squaring of numbers. It includes functions to append input, clear the display, calculate results based on the selected operator, and handle specific operations like remainder, power, and square root. Event listeners are set up for operator buttons to trigger the corresponding calculations.

Uploaded by

sumanthhs2005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

JavaScript Calculator Functions

The document contains JavaScript code for a simple calculator application that allows users to perform basic arithmetic operations, power calculations, square roots, and squaring of numbers. It includes functions to append input, clear the display, calculate results based on the selected operator, and handle specific operations like remainder, power, and square root. Event listeners are set up for operator buttons to trigger the corresponding calculations.

Uploaded by

sumanthhs2005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

const display = document.

getElementById('display');
let currentInput = '';
let operator = null;
let previousInput = '';

function appendInput(value) {
currentInput += value;
[Link] = currentInput;
}

function clearDisplay() {
currentInput = '';
operator = null;
previousInput = '';
[Link] = '';
}

function calculateResult() {
if (currentInput === '' || previousInput === '') return;

let result;
const num1 = parseFloat(previousInput);
const num2 = parseFloat(currentInput);

switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
case '%': // Remainder
result = num1 % num2;
break;
default:
return;
}
[Link] = result;
currentInput = [Link]();
previousInput = '';
operator = null;
}

function setOperator(op) {
if (currentInput === '') return;
if (previousInput !== '') calculateResult(); // Perform previous operation if
any

operator = op;
previousInput = currentInput;
currentInput = '';
}
function calculatePower() {
if (currentInput === '') return;
if (previousInput !== '') {
const num1 = parseFloat(previousInput);
const num2 = parseFloat(currentInput);
const result = [Link](num1, num2);
[Link] = result;
currentInput = [Link]();
previousInput = '';
operator = null;
} else {
alert("Enter the base and then the exponent to calculate power (e.g., 2 x^y
3)");
}
}

function calculateSquareRoot() {
if (currentInput === '') return;
const num = parseFloat(currentInput);
if (num < 0) {
[Link] = "Error";
currentInput = '';
return;
}
const result = [Link](num);
[Link] = result;
currentInput = [Link]();
previousInput = '';
operator = null;
}

function calculateSquare() {
if (currentInput === '') return;
const num = parseFloat(currentInput);
const result = num * num;
[Link] = result;
currentInput = [Link]();
previousInput = '';
operator = null;
}

function calculateRemainder() {
setOperator('%');
}

// Add event listeners for operators (sum, product, difference, quotient)


[Link]('.buttons button').forEach(button => {
const text = [Link];
if (['+', '-', '*', '/'].includes(text)) {
[Link] = () => setOperator(text);
}
});

You might also like