Please help. Calculator. I fixed one issue and whole app crashes. Do not know why

Hi, everyone. I was making the calculator on JS + React. Got 15 / 16 working tests. The one was failed is 13. Fixed it and basically it should work accepting multiple operators and calculating correctly, but currently during the tests app crashes, because it cannot calculator 3+5*6-2 - why? I do not get what happened.

Git: https://github.com/TyroniUA/calculator

Main script:

import React, { createRef } from 'react'
const endsWithOperator = /[*+‑]$/;
const endsWithNegativeSign = /[x/+]‑$/;

class CalculatorTest extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            formula:'',
            input: '0',
            previousValue: '',
        }
        this.handleCLick = this.handleCLick.bind(this);
    }


    handleCLick = (event) => {
        if (this.state.input === '0') {
            //console.log('zero' + this.state.input)
            this.setState({
                formula: '' + event.target.value,
                input: '' + event.target.value
            })

        }
        
        else {
            console.log('HandleCLick else statement')
            this.setState({
                formula: this.state.formula + event.target.value,
                input: this.state.input + event.target.value
            })
        }
    }
    handleOperator =(event)=> {
        console.log(event.target.value)
        if (!endsWithOperator.test(this.state.formula)) {
            console.log('Im testing')
            this.setState({
                previousValue: this.state.formula,
                formula: this.state.formula + event.target.value,
                input: ''
            })
            console.log(this.state.formula);
        }
        
        else if (!endsWithNegativeSign.test(this.state.formula)) {
            console.log('HandleOperator endsWithNegativeSign')
            this.setState({
                formula: (endsWithNegativeSign.test(this.state.formula + event.target.value) ? this.state.formula : this.state.previousValue) + event.target.value,
                })
        }

        else if (event.target.value !== '‑') {
            console.log('it also doing something')
            this.setState({
                formula: this.state.previousValue + event.target.value
            })

        }
    }
    
    addDecimal = (event) => {
        // only add decimal if there is no current decimal point present in the input area
        
        if (this.state.input.indexOf(".") === -1) {
          this.setState({ 
            formula: this.state.formula + event.target.value,  
            input: this.state.input + event.target.value });
        }
      };
    clear = () => {
        this.setState({
            input: '0',
            formula: '0',
            previousValue: '',
            currentValue: ''
        })

}
    
    equals = () => {
       
        // the code that works without trying to solve Problem 13:
        let result = eval(this.state.formula.toString())
        
       this.setState({
           /*formula: this.state.formula + ' = ' + eval(this.state.formula.toString()),*/
           formula: result,
           input: result
        }
       )
       
    }
    render() {

        return (
            <div><h1>TEST</h1>
            <div id='calculator'>
                <div id='formula'>{this.state.formula}</div>
                <div id='display'>{this.state.input}</div>
                {/*console.log(this.state.previousValue, this.state.input)*/}
                <div id='row' >
                    <button id='seven' ref={event => createRef(event)} value='7' onClick={this.handleCLick}>7</button>
                    <button id='eight' ref={event => createRef(event)} value='8' onClick={this.handleCLick}>8</button>
                    <button id='nine' ref={event => createRef(event)} value='9' onClick={this.handleCLick}>9</button>
                    <button id='divide' ref={event => createRef(event)} onClick={this.handleOperator} value='/'>/</button>
                </div>
                <div id='row' >
                    <button id='four' ref={event => createRef(event)} value='4' onClick={this.handleCLick}>4</button>
                    <button id='five' ref={event => createRef(event)} value='5' onClick={this.handleCLick}>5</button>
                    <button id='six' ref={event => createRef(event)} value='6' onClick={this.handleCLick}>6</button>
                    <button id='multiply' ref={event => createRef(event)} onClick={this.handleOperator} value='*'>x</button>
                </div>
                <div id='row' ><button id='one' ref={event => createRef(event)} value='1' onClick={this.handleCLick}>1</button>
                    <button id='two' ref={event => createRef(event)} value='2' onClick={this.handleCLick}>2</button>
                    <button id='three' ref={event => createRef(event)} value='3' onClick={this.handleCLick}>3</button>
                    <button id='add' ref={event => createRef(event)} onClick={this.handleOperator} value='+'>+</button>
                </div>
                <div id='row' >
                    <button id='decimal' ref={event => createRef(event)} onClick={this.addDecimal} value='.'>.</button>
                    <button id='zero' ref={event => createRef(event)} value='0' onClick={this.handleCLick}>0</button>
                    <button id='clear' ref={event => createRef(event)} onClick={this.clear} value='AC' >AC</button>
                    <button id='subtract' ref={event => createRef(event)} onClick={this.handleOperator} value="‑">-</button>
                    </div>
                    <button id='equals' ref={event => createRef(event)} onClick={this.equals} value='='>=</button>
                

            </div>
            </div>
        )
    }
}

export default CalculatorTest

Hello UATyroni,

When posting a question about your code, please include a link to your code/application. Without seeing your app, it is probably impossible to help with your problem.

Sorry, I accidentally pressed enter and was editing: Code and git are in the main post right now. Thank you for helping me!:slight_smile:

Solve it. May be will help to someone: the problem was with synaxis of “” and ‘’ and mismatching elements in regex. Also later I messed with evaluated state but then deleted it.
Code is on git, working code:

import React, { createRef } from 'react'
const endsWithOperator = /[*+-]$/;
const endsWithNegativeSign = /[*/+]-$/;

class Calculator extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            formula:'',
            input: '0',
            previousValue: '',
        }
        this.handleCLick = this.handleCLick.bind(this);
    }


    handleCLick = (event) => {
        if (this.state.input === '0') {
            //console.log('zero' + this.state.input)
            this.setState({
                formula: '' + event.target.value,
                input: '' + event.target.value
            })

        }
        
        else {
            console.log('HandleCLick else statement')
            this.setState({
                formula: this.state.formula + event.target.value,
                input: this.state.input + event.target.value
            })
        }
    }

    // handle operators + - / *
    handleOperator =(event)=> {
        //does it ends with operator
        if (!endsWithOperator.test(this.state.formula)) {
            console.log('Im testing')
            this.setState({
                previousValue: this.state.formula,
                formula: this.state.formula + event.target.value,
                input: event.target.value
            })
        }
        
        // does it ends with negative sign? 
        else if (!endsWithNegativeSign.test(this.state.formula)) {
            console.log('HandleOperator endsWithNegativeSign')
            this.setState({
                formula: (endsWithNegativeSign.test(this.state.formula + event.target.value) ? this.state.formula : this.state.previousValue) + event.target.value,
                })
        }
        
        // if input is not a minus => put it
        else if (event.target.value !=='-') {
            console.log('it also doing something')
            this.setState({
                formula: this.state.previousValue + event.target.value,
                input: event.target.value
            })

        }
    }
    
    addDecimal = (event) => {
        // only add decimal if there is no current decimal point present in the input area
        
        if (this.state.input.indexOf('.') === -1) {
          this.setState({ 
            formula: this.state.formula + event.target.value,  
            input: this.state.input + event.target.value });
        }
      };
    clear = () => {
        this.setState({
            input: '0',
            formula: '',
            previousValue: '',
            
        }
        )
            console.log(this.state.evaluated)
    }

    equals = () => {
       
        // the code that works without trying to solve Problem 13:
        let result = eval(this.state.formula.toString())
        


       this.setState({
           /*formula: this.state.formula + ' = ' + eval(this.state.formula.toString()),*/
           formula: result,
           input: result,
           previousValue: result,
        }
       )
       
    }
    render() {
        console.log(this.state.formula);
        return (
            <div><h1>Hi, I'm a calculator</h1>
            <div id='calculator'>
                <div id='formula'>{this.state.formula}</div>
                <div id='display'>{this.state.input}</div>
                {/*console.log(this.state.previousValue, this.state.input)*/}
                <div id='row' >
                    <button id='seven' ref={event => createRef(event)} value='7' onClick={this.handleCLick}>7</button>
                    <button id='eight' ref={event => createRef(event)} value='8' onClick={this.handleCLick}>8</button>
                    <button id='nine' ref={event => createRef(event)} value='9' onClick={this.handleCLick}>9</button>
                    <button id='divide' ref={event => createRef(event)} onClick={this.handleOperator} value='/'>/</button>
                </div>
                <div id='row' >
                    <button id='four' ref={event => createRef(event)} value='4' onClick={this.handleCLick}>4</button>
                    <button id='five' ref={event => createRef(event)} value='5' onClick={this.handleCLick}>5</button>
                    <button id='six' ref={event => createRef(event)} value='6' onClick={this.handleCLick}>6</button>
                    <button id='multiply' ref={event => createRef(event)} onClick={this.handleOperator} value='*'>x</button>
                </div>
                <div id='row' ><button id='one' ref={event => createRef(event)} value='1' onClick={this.handleCLick}>1</button>
                    <button id='two' ref={event => createRef(event)} value='2' onClick={this.handleCLick}>2</button>
                    <button id='three' ref={event => createRef(event)} value='3' onClick={this.handleCLick}>3</button>
                    <button id='add' ref={event => createRef(event)} onClick={this.handleOperator} value='+'>+</button>
                </div>
                <div id='row' >
                    <button id='decimal' ref={event => createRef(event)} onClick={this.addDecimal} value='.'>.</button>
                    <button id='zero' ref={event => createRef(event)} value='0' onClick={this.handleCLick}>0</button>
                    <button id='clear' ref={event => createRef(event)} onClick={this.clear} value='AC' >AC</button>
                    <button id='subtract' ref={event => createRef(event)} onClick={this.handleOperator} value='-'>-</button>
                    </div>
                    <button id='equals' ref={event => createRef(event)} onClick={this.equals} value='='>=</button>
            </div>
            </div>
        )
    }
}

export default Calculator