From 187a83302ee662a52f06f3837b9f78a57d45d1b5 Mon Sep 17 00:00:00 2001 From: Kevin Paul Date: Thu, 2 Nov 2023 22:44:43 +0530 Subject: [PATCH] fixed trigno and log functions --- script.js | 240 ++++++++++++++++++++---------------------------------- 1 file changed, 89 insertions(+), 151 deletions(-) diff --git a/script.js b/script.js index 48f9905..1a13928 100644 --- a/script.js +++ b/script.js @@ -1,12 +1,12 @@ // Initialize an empty string to store the user input let inputString = ''; +let mathExpression = ''; // Select the input field const inputField = document.querySelector('input'); const resultDisplay = document.querySelector('#results'); const operators = ['+','-','*','/']; - // Select all the calculator buttons on the page const calculatorButtons = document.querySelectorAll('button'); @@ -37,19 +37,98 @@ function getRandomColor() { for (let i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } - color += '80'; // so that the background is not a solid color return color; } -// Function to change the background color -function changeBackgroundColor() { - bodyElement.style.background = generateRandomGradient(); - parentBox.classList.add('box-highlight'); +// Add an event listener to each button +calculatorButtons.forEach((button) => { + button.addEventListener('click', () => { + const buttonValue = button.textContent; - // Add a button press effect to the parent container - setTimeout(() => { - parentBox.classList.remove('box-highlight'); - }, 200); + switch (buttonValue) { + case 'AC': + case 'CE': + inputField.value = ''; + mathExpression = ''; + break; + case 'C': + if (inputField.value.slice(-1) === ')') { + const lastOpenParenthesisIndex = inputField.value.lastIndexOf('('); + inputField.value = inputField.value.slice(0, lastOpenParenthesisIndex); + mathExpression = mathExpression.slice(0, lastOpenParenthesisIndex); + } else { + inputField.value = inputField.value.slice(0, -1); + mathExpression = mathExpression.slice(0, -1); + } + break; + case '=': + calculateResult(); + break; + case 'sin': + inputField.value += 'sin('; + mathExpression += 'Math.sin('; + break; + case 'cos': + inputField.value += 'cos('; + mathExpression += 'Math.cos('; + break; + case 'tan': + inputField.value += 'tan('; + mathExpression += 'Math.tan('; + break; + case 'log': + inputField.value += 'log('; + mathExpression += 'Math.log('; + break; + case 'sqrt': + inputField.value += 'sqrt('; + mathExpression += 'Math.sqrt('; + break; + case '^': + inputField.value += '^'; + mathExpression += '**'; + break; + case 'π': + inputField.value += 'π'; + mathExpression += 'Math.PI'; + break; + case 'e': + inputField.value += 'e'; + mathExpression += 'Math.E'; + break; + default: + // Append the clicked button's value to the input field and the math expression + inputField.value += buttonValue; + mathExpression += buttonValue; + break; + } + }); +}); + +// Function to calculate and display the result +function calculateResult() { + try { + // Evaluate the math expression using eval + const result = eval(mathExpression); + + // Display the result in the second input field + animateValueChange(resultDisplay, result, 500); + + // Reset the math expression + mathExpression = ''; + + // Clear the input field + inputField.value = ''; + + // Change the background color + bodyElement.style.background = generateRandomGradient(); + } catch (error) { + // Handle invalid expressions + navigator.vibrate(200); + inputField.value = ''; + mathExpression = ''; + resultDisplay.value = error.message; // Clear the dashboard on error + } } function animateValueChange(element, end, duration) { @@ -80,147 +159,6 @@ function animateValueChange(element, end, duration) { window.requestAnimationFrame(step); } - - -// Function to calculate and display the result -function calculateResult() { - const inputValue = inputField.value; - try { - // Replace percentage symbol (%) with '/100' before evaluation - inputString = inputValue.replace(/%/g, '/100'); - // Replace √(first digit group) with 'Math.sqrt()' function for that digit group before evaluation - inputString = inputValue.replace(/√([+-]*\d+)/g, 'Math.sqrt($1)'); - - - // Evaluate the user input as a mathematical expression using eval - const result = eval(inputString); - - // Display the result in the second input field - animateValueChange(resultDisplay, result, 500); - - // Revert back the Math.sqrt() function - inputString = inputValue.replace(/Math.sqrt\((\d+)\)/g, '√$1'); - - // Change the background color here - changeBackgroundColor(); - } catch (error) { - // Handle invalid expressions - navigator.vibrate(200); - inputField.value = ''; - inputString = ''; - resultDisplay.value = error.message; // Clear the dashboard on error - } -} - -// Function to continuously update the result as the user types or presses buttons -function updateResult() { - inputString = inputField.value; - - try { - // Check if the inputString is not empty and contains a valid expression - if (inputString) { - const result = eval(inputString); - animateValueChange(resultDisplay, result, 500); - } else { - resultDisplay.value = ''; // Clear the dashboard if the input is empty - } - } catch (error) { - resultDisplay.value = ''; // Clear the dashboard on error - } -} - -// Add an event listener to each button -calculatorButtons.forEach((button) => { - button.addEventListener('click', () => { - const buttonValue = button.textContent; - - switch (buttonValue) { - case '=': - // Check if the inputString is not empty and contains a valid expression - if (inputString) { - calculateResult(); - } - break; - case 'C': - // Clear the inputString and reset the input field - inputString = ''; - inputField.value = inputString; - // claer the Display field - resultDisplay.value = ''; - break; - case 'M+': - // Add the current input value to the memory value - memoryValue += parseFloat(inputField.value) || 0; - inputString = ''; - break; - case 'M-': - // Subtract the current input value from the memory value - memoryValue -= parseFloat(inputField.value) || 0; - inputString = ''; - break; - case 'sin': - inputField.value = Math.sin(parseFloat(inputField.value)); - inputString = inputField.value.toString(); - break; - case 'cos': - inputField.value = Math.cos(parseFloat(inputField.value)); - inputString = inputField.value.toString(); - break; - case 'tan': - inputField.value = Math.tan(parseFloat(inputField.value)); - inputString = inputField.value.toString(); - break; - case '^': - const num = parseFloat(inputField.value); - const result = num * num; - //inputString = result.toString(); - break; - case '√': - // inputField.value = Math.sqrt(parseFloat(inputField.value)); - - // Check if input field value is empty and if it is, not then √ should precede * - const inputFieldValueLength = inputField.value.length; - if(inputFieldValueLength != 0 && !operators.includes(inputField.value[inputFieldValueLength - 1])) - inputField.value += '*'; - inputField.value += '√'; - inputString = inputField.value.toString(); - - break; - case 'log': - inputField.value = Math.log(parseFloat(inputField.value)); - inputString = inputField.value.toString(); - break; - case 'π': - inputField.value += Math.PI.toFixed(3); - inputString = inputField.value.toString(); - break; - case 'e': - inputField.value = Math.E; - inputString = inputField.value.toString(); - break; - case 'X!': - const numX = parseFloat(inputField.value); - let factorial = 1; - for (let i = 1; i <= numX; i++) { - factorial *= i; - } - inputString = factorial.toString(); - break; - case 'CE': - inputField.value = inputField.value.slice(0, -1); - inputString = inputField.value; - break; - default: - // Append the clicked button's value to the input field - inputString += buttonValue; - inputField.value = inputString; - updateResult(); // Update the result as you add operators - break; - } - }); -}); -inputField.addEventListener('input', updateResult); - // Set the date we're counting down to var countDownDate = new Date("Oct 31, 2023 23:59:59").getTime();