简体   繁体   English

如何让 javascript 计算器中的加减函数正常工作?

[英]How can I get add and subtract functions in javascript calculator to work correctly?

I am making a basic calculator in javascript.我正在用 javascript 制作一个基本的计算器。 My addition and multiplication buttons work well.我的加法和乘法按钮运行良好。 However, my subtraction and division functions are not working.但是,我的减法和除法函数不起作用。

When I click subtract, for some reason that I can't figure out (I've been scratching my head forever), it automatically converts the number in the output to a negative number.当我单击减法时,由于某种我无法弄清楚的原因(我一直在挠头),它会自动将输出中的数字转换为负数。

And for division, I can't seem to get the logic down to divide the first number I enter by the second number I enter.对于除法,我似乎无法理解将我输入的第一个数字除以我输入的第二个数字的逻辑。

Here is my basic calculate function:这是我的基本计算功能:

function calculate() {
    if (operator == 'add') {
        runningSum += windowNum;
    } else if (operator == 'subtract') {
        runningSum -= windowNum;  // automatically converts windowNum to negative, unclear why
    } else if (operator == 'multiply') {
        if (runningSum == 0) {
            runningSum = 1;
        }
        runningSum *= windowNum;
    } else if (operator == 'divide') {
        // ever mutation tried comes up with wrong result
    }

    outputWindow.textContent = Number((runningSum).toFixed(5));
    operatorClicked = true;
    numClicked = false;
    document.querySelector('#btnAdd').classList.remove('operatorClicked');
    console.log('windowNum: ' + windowNum);
    console.log('runningSum: ' + runningSum);
}

Because my project is rather big, I've included a link to it in codepen here: https://codepen.io/CDLWebDev/pen/mdJgbeG因为我的项目相当大,我在 codepen 中包含了它的链接: https ://codepen.io/CDLWebDev/pen/mdJgbeG

I checked your codepen and changed a few things.我检查了您的 codepen 并更改了一些内容。 Mainly, you were performing calculations whenever a click to the operation signs was made, which is not really something you want to do, since you can't perform operations when you don't know what number will come next.主要是,每当单击操作标志时,您就在执行计算,这不是您真正想要做的事情,因为当您不知道接下来会出现什么数字时,您无法执行操作。 On calculators, calculations are actually performed when you press the "equals" sign.在计算器上,当您按下“等号”时,实际上会进行计算。

What really has to happen and what I've done in the code below is keep the number you just pressed as your runningSum and choose the operation, then when you press equal you have all the info you need.真正必须发生的事情以及我在下面的代码中所做的是保留您刚刚按下的数字作为 runningSum 并选择操作,然后当您按下等于时,您将获得所需的所有信息。

https://codepen.io/VPR/pen/poJBzXP https://codepen.io/VPR/pen/poJBzXP

function clickOperatorBtn() {
if (numClicked) {
  if (target == document.querySelector("#btnDivide")) {
    operator = "divide";
    runningSum = windowNum;
    clearWindow();
  } else if (target == document.querySelector("#btnMultiply")) {
    operator = "multiply";
    runningSum = windowNum;
    clearWindow();
  } else if ...

I assume this is a learning exercise, so keep it up, but I think the logic behind your code could improve, when you're done try googling some tutorials on calculators, you'll find many which walk you through the steps performed and the logic behind it.我认为这是一个学习练习,所以坚持下去,但我认为你的代码背后的逻辑可以改进,当你完成后尝试谷歌搜索一些关于计算器的教程,你会发现很多指导你完成执行的步骤和背后的逻辑。

I tried your code and the function calculate is ran every time you change the operator.我尝试了您的代码,每次更改运算符时都会运行calculate函数。 That means that when, initially, click on the '-' sign you will trigger that function.这意味着,当最初单击“-”符号时,您将触发该功能。 Bear with me:跟我来:

else if (operator == 'subtract') {
    // windowNum == 3 - for example
    // runningSum == 0 
    runningSum -= windowNum;
    // result will be 0 - 3 == -3

This means that if you do the same with say, 7. You'll be doing -3 - 7 == -10这意味着如果你对 say, 7 做同样的事情。你会做 -3 - 7 == -10

About the division: This is also happening, so when you do something like clicking 8 and then division, what you're doing is 0 / 8 (which apparently results in 1).关于除法:这也发生了,所以当你点击 8 然后除法时,你所做的是0 / 8 (显然结果是 1)。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能让这个 Javascript 计算器工作? - How can I get this Javascript calculator to work? 如何在 JavaScript 中正确地减去分钟? - How can I correctly subtract minutes from each other in JavaScript? 如何使用JavaScript加,减或乘两个变量? - How can I add, subtract, or multiply two variables using JavaScript? 如何使用javascript通过按钮向计数器添加或减去数字? - How can I add or subtract numbers to a counter with a button using javascript? 如何使用 JavaScript 计算器将“分数”添加到输入区域? - How can I add “Fractions” to input area with JavaScript calculator? Java 脚本功能不起作用,如何将我的 javascript 文件添加到 html 并使其工作 - Java Script Functions Don't work and How do i add my javascript file to html and get it to work 如何让$ .getScript正常工作? - How can I get $.getScript to work correctly? 我怎样才能让这些 Javascript 函数在不与我的原始变量结合的情况下正确递增? - How can i get these Javascript functions to increment correctly without combining with my original variable? 如何使用javascript在计算器中添加小数? - How can add decimal in calculator with javascript? 我刚开始学习 javascript,我正在尝试制作一个计算器网站,但无法让它工作 - i just started learning javascript and i'm trying to make a calculator website but can't get it to work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM