javascript - 我在 vue 中做了一个计算器,我希望它能够输入最后一个运算符并更改前一个(js 有问题)
问题描述
我有这个数据
value: '',
numbers: ['C','*','/','-',7,8,9,'+',4,5,6,'%',1,2,3,'=',0,'.'],
operator: '',
newvalue: '',
实际上,当您输入“8+9”时它正在工作,但我希望我的计算器在您输入“8+-”时更改运算符,它必须删除 + 并将其替换为 - 但我的代码的工作方式类似于“8+-9” " 用这个 if 语句
if (['/','*','-','+','%'].includes(x)) {
this.operator = x;
if (this.value == this.operator) {
this.value = this.operator
}else {
this.value += this.operator;
}
}
if (x === '=') {
this.value = eval(
this.newvalue + this.operator + this.value
);
this.newvalue = '';
this.operator = '';
}
}
这是我在 github 上的完整代码,如果有人能提供帮助,我将不胜感激 https://github.com/rozhansh43/vue-calculator/blob/master/src/components/Calculator.vue
解决方案
推荐阅读
- google-maps - Google 地图徽标归属政策
- firebase - 是否可以从 firebase 向电话号码发送短信?
- python - 如何在 Python 3.x 中从字符串实例化函数?
- ios - 在模拟器+物理设备上运行的应用程序突然黑屏
- spring - 从默认错误列表中删除对象 Spring boot 验证
- java - 在jframe上显示图像
- csv - 用于替换文件夹所有 CSV 中的字符的批处理文件
- spring-mvc - 出现错误:创建类路径资源中定义的名称为“entityManagerFactory”的bean时出错
- java - 从列表中获取文本
在 Groovy 中 - apache-spark - Spark:如何在 spark-submit 中设置 spark.yarn.executor.memoryOverhead 属性