Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in eval() #14

Open
surendragalwa11 opened this issue Nov 14, 2018 · 0 comments
Open

Error in eval() #14

surendragalwa11 opened this issue Nov 14, 2018 · 0 comments

Comments

@surendragalwa11
Copy link

surendragalwa11 commented Nov 14, 2018

2+-3 or 2/3 and these kind of expressions are causing error in eval()*

import React, { Component } from 'react';
import update from 'immutability-helper';
import math from 'mathjs';
import './App.css';
import Display from './Display';
import Button from './Button';
import Buttons from './Buttons';

class App extends Component
{
  constructor()
  {
    super();
    this.state={
           operations: []
    };
  }

  calculateOperations = () => {
    var result = this.state.operations.join('');
    if(result)
    {
      result = String(math.eval(result));
      this.setState({ operations : [result] });
    }
    
  }

  handleClick = e => {
    var value = e.target.getAttribute('data-value');
    switch (value)
    {
      case 'clear': this.setState({ operations: []});  break;
      case 'equal': this.calculateOperations();  break;
      default: var newOperations = update(this.state.operations, {$push: [value]});
               this.setState({operations: newOperations}); break;
    }
  }

  render()
  {
    return (
      <div className="App">
        <Display data={this.state.operations} />
        <Buttons>
          <Button onClick={this.handleClick} label="C" value="clear" />
          <Button onClick={this.handleClick} label="7" value="7" />
          <Button onClick={this.handleClick} label="4" value="4" />
          <Button onClick={this.handleClick} label="1" value="1" />
          <Button onClick={this.handleClick} label="0" value="0" />
          
          <Button onClick={this.handleClick} label="/" value="/" />
          <Button onClick={this.handleClick} label="8" value="8" />
          <Button onClick={this.handleClick} label="5" value="5" />
          <Button onClick={this.handleClick} label="2" value="2" />
          <Button onClick={this.handleClick} label="." value="." />
          
          <Button onClick={this.handleClick} label="x" value="*" />
          <Button onClick={this.handleClick} label="9" value="9" />
          <Button onClick={this.handleClick} label="6" value="6" />
          <Button onClick={this.handleClick} label="3" value="3" />
          <Button label="" value="null" />
          
          <Button onClick={this.handleClick} label="-" value="-" />
          <Button onClick={this.handleClick} label="+" size="2" value="+" />
          <Button onClick={this.handleClick} label="=" size="2" value="equal" />
        </Buttons>
      </div>
    );
  }
}

export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant