Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
Having trouble with eval() with React, it won't actually evaluate the string

I am trying to build the calculator from Freecodecamp's front end projects. And I am using the eval() function with pressing the equals button.

Here is the child component

    class Equals extends React.Component {
  constructor(props) {
    super(props);
    this.handleEquals = this.handleEquals.bind(this)
  }
  
  handleEquals = () => {
    this.props.handleClick(eval(this.props.exp))
  }
  
  render() {
    return (
      <button id='equals' onClick={this.handleEquals}>=</button>
    )
  }
}

And here is the parent component

class Calculator extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      numPad: numPad,
      operations: operations,
      prevVal: [],
      currVal: '',
      display: '',
      expression: []
    }
    this.handleDisplay = this.handleDisplay.bind(this);
    this.clearDisplay = this.clearDisplay.bind(this);

  }
  
  handleDisplay = input => {
    
    this.setState({
      currVal: input,
      prevVal: this.state.prevVal.concat(this.state.currVal),
      display: this.state.prevVal.concat(this.state.currVal).concat(input),
      expression: this.state.prevVal.concat(this.state.currVal).concat(input)
    
  })
  }
  
  handleEvaluate = input => {
    this.setState({
      display: input
    })
  }
  
  clearDisplay() {

    this.setState({
      prevVal: '',
      currVal: '',
      display: '0'
    })
  }
  render() {
    return (
      <div>
        <div id='display'>{this.state.display}</div>
        <Equals exp = {this.state.expression} handleClick = {this.handleEvaluate}/> 
        {this.state.numPad.map(num => 
                              <NumPad dig={num.dig} wrd={num.wrd} handleClick={this.handleDisplay}/>)}
        {this.state.operations.map(op => 
                              <Operations sym={op.sym} wrd={op.wrd} handleClick={this.handleDisplay}/>)}
        <button id='decimal'>.</button>
        <button id='clear' onClick={this.clearDisplay}>Clear</button>
        
      </div>
    )
  }
}

When I press the equals sign I get the same expression back. I have tried passing 'this.props.exp' and this.props.exp.toString() into the eval() function but it is still giving me the same string back without evaluating it.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Apologies, i should specify that expression is based on an array of values created by clicking buttons with 0-9 and the 4 basic expressions. I then pass that array into the eval() function using this.props.exp.join(""). I was using toString() forgeting that it puts commas between all the elements.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda