-2
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!
'''import React, { Component } from 'React' import Button from '.. /Components/Button import Display from '.. /Components/Display' import './Calculator.css'
const initialState = { displayValue: '0', clearDisplay: false, Operation: null, values: [0,0], Current: 0,
} Function percentage (displayValue, values) {
}
export default class Calculator extends Component {
state = {...initialState}
constructor(props){
super(props)
this.clearMemory = this.clearMemory.bind(this)
this.setOperation = this.setOperation.bind(this)
this.addDigit = this.addDigit.bind(this)
}
clearMemory(){
this.setState({...initialState})
}
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
})
} if (Button === "%"){
if (operation && displayValue){
const current = operation(current, displayValue, operation);
return {
current : null,
displayValue: null,
}
}
}
}
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 })
}
}
render() {
return(
<div className='calculator'>
<Display value = {this.state.displayValue}/>
<Button label = 'AC'click={this.clearMemory} triple/>
<Button label = '/' click={this.setOperation} operation/>
<Button label = '%' click={this.setOperation} operation/>
<Button label = '-' click={this.setOperation} operation/>
<Button label = '7' click={this.addDigit}/>
<Button label = '8' click={this.addDigit}/>
<Button label = '9' click={this.addDigit}/>
<Button label = '+' click={this.setOperation} operation/>
<Button label = '4' click={this.addDigit}/>
<Button label = '5' click={this.addDigit}/>
<Button label = '6' click={this.addDigit}/>
<Button label = '*' click={this.setOperation} operation/>
<Button label = '1' click={this.addDigit}/>
<Button label = '2' click={this.addDigit}/>
<Button label = '3' click={this.addDigit}/>
<Button label = '.' click={this.addDigit}/>
<Button label = '+/-' click={this.setOperation} operation/>
<Button label = '0' click={this.addDigit}/>
<Button label = ',' click={this.addDigit}/>
<Button label = '=' click={this.setOperation} operation/>
</div>
)
}
}'''
To proceed with doubt it is important [Dit] to post to remove this code and instead provide a [mcve] of the problem, and an objective explanation. To understand what kind of question serves the site and, consequently, avoid closures and negativities worth reading What is the Stack Overflow and the Stack Overflow Survival Guide (summarized) in Portuguese.
– Bacco