简体   繁体   English

Javascript计算器如何允许多个点

[英]Javascript calculator how to allow multiple dots

So basically this project is almost done.所以基本上这个项目就差不多完成了。 But when I was 'testing' it, I found out, that I can't create multiple dots.但是当我“测试”它时,我发现我无法创建多个点。 I know why I can't do that:我知道为什么我不能这样做:

// if there is any dot, and user want to add another dot, then return
    if(containDot && operand == '.') return;

So, the problem is that I have no other idea, how can I block adding multiple dots on ONE number and at the same time allow adding it into another numbers with idea for making operations with eval();所以,问题是我没有其他想法,我怎样才能阻止在一个数字上添加多个点,同时允许将它添加到另一个数字中,以便用 eval() 进行操作;

Here's link to code: https://codepen.io/Igor34/pen/LYyoZMR这是代码链接: https://codepen.io/Igor34/pen/LYyoZMR

I modified your code to split the text into each number and look for a dot only on the last number.我修改了您的代码以将文本拆分为每个数字并仅在最后一个数字上寻找一个点。

const currentOperator = this.currentScreen.textContent.split(/[-+\\/\\*]/).pop();
// if there is any dot, and user want to add another dot, then return
if (currentOperator.includes(".") && operand === ".") return

// LINE BELOW REMOVED
// if(containDot && operand == '.') return;

See https://codepen.io/jmendes/pen/XWRwjJm参见https://codepen.io/jmendes/pen/XWRwjJm

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM