Percentage Calculation React.jsx

Asked

Viewed 14 times

0

Good afternoon, I’m new here.

I’m racking my brain to find out how I’m going to be able to do a percentage calculation on the calculator I’m creating, I can’t make it happen. The other operations are all working properly, now percentage I can’t, I’m already 2 days into this, could someone please help me. Thanks in advance!

**IF IT HELPS TO UNDERSTAND MY CODE:

setOperation(operation) {
        if (this.state.current === 0) {
            this.setState({ operation, current: 1, clearDisplay: true })
        } else {
            const equals = operation === '='
            const currentOperation = this.state.operation
            const values = [...this.state.values]
            try {
                values[0] = eval(`${values[0]} ${currentOperation} ${values[1]}`)
            } catch(e) {
                values[0] = this.state.values[0]
            }
            values[1] = 0
            
            this.setState({
                displayValue: values[0],
                operation: equals ? null : operation,
                current: equals ? 0 : 1,
                clearDisplay: !equals,
                values
            })
        } 
    }
    
    addDigit(n) {
        if (n === '.' && this.state.displayValue.includes('.')){
            return;
        }
        const clearDisplay = this.state.displayValue ==='0' || this.state.clearDisplay
        const currentValue = clearDisplay ? '' : this.state.displayValue
        const displayValue = currentValue + n
        this.setState({displayValue, clearDisplay: false})
        if (n !== '.'){
            const i = this.state.current
            const newValue = parseFloat(displayValue)
            const values = [...this.state.values]
            values[i] = newValue
            this.setState({ values })
            
        }
    }**
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.