Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > General Coding
You last visited: Today at 03:45

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



JS Calculator Program Error

Discussion on JS Calculator Program Error within the General Coding forum part of the Coders Den category.

Reply
 
Old   #1
 
gulshan212's Avatar
 
elite*gold: 0
Join Date: Apr 2023
Posts: 5
Received Thanks: 0
JS Calculator Program Error

Hello this is Gulshan Negi
I am writing a JavaScript program for making a calculator but it shows some error at the time of execution.
Source Code:

const calculator = {
displayValue: '0',
firstOperand: null,
waitingForSecondOperand: false,
operator: null,
};

function inputDigit(digit) {
const { displayValue, waitingForSecondOperand } = calculator;

if (waitingForSecondOperand === true) {
calculator.displayValue = digit;
calculator.waitingForSecondOperand = false;
} else {
calculator.displayValue = displayValue === '0' ? digit : displayValue + digit;
}
}

function inputDecimal(dot) {
if (calculator.waitingForSecondOperand === true) {
calculator.displayValue = "0."
calculator.waitingForSecondOperand = false;
return
}

if (!calculator.displayValue.includes(dot)) {
calculator.displayValue += dot;
}
}

function handleOperator(nextOperator) {
const { firstOperand, displayValue, operator } = calculator
const inputValue = parseFloat(displayValue);

if (operator && calculator.waitingForSecondOperand) {
calculator.operator = nextOperator;
return;
}


if (firstOperand == null && !isNaN(inputValue)) {
calculator.firstOperand = inputValue;
} else if (operator) {
const result = calculate(firstOperand, inputValue, operator);

calculator.displayValue = `${parseFloat(result.toFixed(7))}`;
calculator.firstOperand = result;
}

calculator.waitingForSecondOperand = true;
calculator.operator = nextOperator;
}

function calculate(firstOperand, secondOperand, operator) {
if (operator === '+') {
return firstOperand + secondOperand;
} else if (operator === '-') {
return firstOperand - secondOperand;
} else if (operator === '*') {
return firstOperand * secondOperand;
} else if (operator === '/') {
return firstOperand '/' secondOperand;
}

return secondOperand;
}

function resetCalculator() {
calculator.displayValue = '0';
calculator.firstOperand = null;
calculator.waitingForSecondOperand = false;
calculator.operator = null;
}

function updateDisplay() {
const display = document.querySelector('.calculator-screen');
display.value = calculator.displayValue;
}

updateDisplay();

const keys = document.querySelector('.calculator-keys');
keys.addEventListener('click', event => {
const { target } = event;
const { value } = target;
if (!target.matches('button')) {
return;
}

switch (value) {
case '+':
case '-':
case '*':
case '/':
case '=':
handleOperator(value);
break;
case '.':
inputDecimal(value);
break;
case 'all-clear':
resetCalculator();
break;
default:
if (Number.isInteger(parseFloat(value))) {
inputDigit(value);
}
}

updateDisplay();
});

I also checked and take code reference from , but I don't know what I am doing wrong here. Can anyone give their suggestions on this.
Thanks
gulshan212 is offline  
Old 04/14/2023, 10:33   #2
 
kissein's Avatar
 
elite*gold: 0
Join Date: Sep 2005
Posts: 427
Received Thanks: 87
Code:
function calculate(firstOperand, secondOperand, operator) {
    if (operator === '+') {
        return firstOperand + secondOperand;
    } else if (operator === '-') {
        return firstOperand - secondOperand;
    } else if (operator === '*') {
        return firstOperand * secondOperand;
    } else if (operator === '/') {
        return firstOperand '/' secondOperand; <-- this needs to be / without quotationmarks
    }

    return secondOperand;
}
kissein is offline  
Old 04/21/2023, 06:25   #3
 
gulshan212's Avatar
 
elite*gold: 0
Join Date: Apr 2023
Posts: 5
Received Thanks: 0
Thanks a lot for your kind response, I will try it.
Thanks again.
gulshan212 is offline  
Reply

Tags
javascipt, program, project


Similar Threads Similar Threads
Elo boost price calculator? Elo boost price calculator?
07/15/2021 - League of Legends - 1 Replies
i need Elo boost price calculator, same: https://l9eloboosting.com/order https://eloboost24.eu/pt/boosting/solo/division
[Release] White stats calculator program ( Can be used with PHP ).
06/25/2016 - SRO PServer Guides & Releases - 16 Replies
Hello, This is my first release on elitepvpers, I hope you will like it. So today I'm going to release a small program, made by me. This program calculates white stats of any item you give it, take a look on the PHP code and you will understand how simple is to use that. Most of you use PHP 32 BIT, in PHP 32 BIT you don't have the abilities to do what the program does, PHP 32 BIT can't get high numbers into int variables, so you have to use this program for make it works perfectly. I...
DARKORBIT DMG Calculator Program
01/08/2013 - DarkOrbit - 11 Replies
https://www.virustotal.com/file/87f605b629d2f92f6e 61dcddefcf4e5c1f1b7cf0ea9a321cb93c83039d34e5ed/ana lysis/1357604894/ darkorbit_calculator_v.2.4.zip Here is a darkorbit dmg calculator :D I Hope you like it ! :o
Can someone get me the [Program] EDR - Easy DarkOrbit Rank Calculator!
05/13/2012 - DarkOrbit - 6 Replies
Can someone get me the EDR - Easy DarkOrbit Rank Calculator!thank you!
Age of Conan Feat Calculator - AoC Feat Calculator
06/13/2010 - General Gaming Releases - 3 Replies
the most accurate Age of Conan Feat Calculator



All times are GMT +1. The time now is 03:46.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.