【问题标题】:SyntaxError: invalid increment/decrement operandSyntaxError:无效的递增/递减操作数
【发布时间】:2022-03-02 03:39:21
【问题描述】:

我试图在 react 中创建一个计算器,但是当我进行计算时,它给了我语法错误。例如,当计算不同的数字天(30 + 23423 - 3428 *5235)时,它给了我这个错误。我也无法让我的重置和退格和小数工作。有人可以帮忙吗

这是我的代码

import React, { Component } from 'react'
import Button from "./components/Button";
import Input from "./components/Input";
import {buttonData} from "./components/Data"


class App extends Component {
    constructor(props) {
        super(props)
        this.state = {
            input: [],
            result: 0,
            operator: "+"
        }
    }
    calculate = (num1, operator, num2) => {
        let result;
        // console.log("math");
        if(!isNaN(num2)) {
        result = eval(num1 + operator + num2) 
        this.setState({result})
        }
    }

    handleClick = (button) => {
    let input = [...this.state.input]
    let result = this.state.result;
    let operator = this.state.operator;
    const num = parseFloat(input.join(''))

    switch(true) {
        case /[+-\/\*]/.test(button):
        // console.log(operator);
        this.calculate(result, operator, num)
        operator = button;
        input = operator;
        break

        case/\d/.test(button):
        // console.log(button);
        if(/[+-\/\*]/.test(button)){
            input = []
            }
            input.push(button)
        break
    }
    
    if(button === "="){
        this.calculate(result, operator, num)
    }
    else if(button === "C"){
        this.reset()
    }
    else if(button === "CE"){
        this.backspace()
    }
    else if(button === "."){
        this.handleDecimal()
    }
    this.setState({input, operator})
};


    
    reset = () => {
        this.setState({ 
            input: "0",
            reset: []
        })
    };
    
    backspace = () => {
    this.setState({
        input: this.state.input.slice(0, -1)
    })
    };
    
    handleDecimal = () => {
    const { input } = this.state;
    // ! add the point only if no point is already included
    if(!/\./.test(input)) {
        this.setState({input: `${input}.`})
    }
    }
    
    
    
    render() {
        return (
        <div className="app">
        <div className="calc-wrapper">
            <h2>Tony's Calculator</h2>
            <div id="display" >
            <Input input={this.state.input}
            result={this.state.result}
            />
            </div>
            {buttonData.map((item) => {
                let name = item.button;
                let id = item.id;
            return <Button 
            key={id} 
            name={name} 
            id={id}
            value={name} 
            handleClick={this.handleClick} 
            />
            } )
            } 
        </div>
    </div>
        )
    }
}

export default App;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

enter link description here

【问题讨论】:

  • 你从哪里得到这个错误?请发布minimal reproducible example 展示相关代码,因为很难调试整个应用程序
  • 计算函数出错
  • 这个函数的输入是什么并导致问题?
  • 我不知道怎么回答,但是已经链接了 github,你可以运行代码看看在说什么
  • 我现在无法运行此代码。答案很简单 - 您 能够运行它,因此调试应用程序并在出现错误时检查 num1、operator 和 num2 是什么。我怀疑它类似于num1="1 +"、operator="+"、num2="2"。但我可能错了。我很难单独使用代码来追踪会发生什么。

标签: javascript reactjs react-class-based-component


【解决方案1】:

当您尝试将 eval() 或 alert() 与包含 -- 或 的数学表达式一起使用时会发生错误>++

例如:

alert(-50--30.25*32)

将抛出:Uncaught SyntaxError: invalid increment/decrement operand

因此,您需要用括号括住您的值或用单个符号替换您的符号。

例如:

alert(-50-(-30.25)*32)

或者:

str.replace(/[-]{2}|[+]{2}/g, '+')

如果我正在构造一个字符串,我会使用括号,如果我正在读取一个字符串,我会使用正则表达式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 2013-01-27
    相关资源
    最近更新 更多