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

384
Visualizações
why is the switch function not working in reactjs?

I cannot get why I get an error message (the default case) even I if I clicked on + - ÷ * ?? is not it supposed to set the state like i defined in the switch case ?I thought it would set the state

here is my code please tell me what I did wrong??

class App extends React.Component {
  constructor(props){
    super(props);
    this.state={
      num:[],
      sign:"",
      res:0
    }
    this.handleclick=this.handleclick.bind(this);
   this.handelAssign=this.handelAssign.bind(this);

  }
  handleclick(ele) {
    this.setState({ num: [...this.state.num, ele] });
    this.handelAssign(ele);
    
  }; 
   handelAssign = (operator) => {
    /**operatior could be + - * ÷ */
    switch (operator) {
      case operator === "+":
        this.setState({ operator: "+" });
        break;
      case operator === "-":
        this.setState({ operator: "-" });
        break;
      case operator === '*':
        this.setState({ operator: "*" });
        break;
      case operator === "÷":
        this.setState({ operator: "/" });
        break;
      default: 
        throw new Error('sorry you must enter a valid operator')
    }};

  render() { 
    const buttons = ["÷", 1, 2, 3, "*", 4, 5, 6,"+", 7, 8, 9, ".", 0, "-"];
      let allbuttons="";
    
        
    return (
      <div className="calculation-grid">
        <div className="output">
          <div className="previous-operand">{this.state.num}</div>
          <div className="current-operand"></div>
        </div>
        <button className="span-two">AC</button>
        <button>DEL</button>
       {allbuttons=buttons.map((ele)=><button  onClick={()=>this.handleclick(ele) }  key={ele}>{ele}</button>)}
        <button className="span-two">=</button>

</div>
    );
  }
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Could you try with this :

   handelAssign = (operator) => {
    /**operatior could be + - * ÷ */
    switch (operator) {
      case "+":
        this.setState({ operator: "+" });
        break;
      case "-":
        this.setState({ operator: "-" });
        break;
      case '*':
        this.setState({ operator: "*" });
        break;
      case "÷":
        this.setState({ operator: "/" });
        break;
      default: 
        throw new Error('sorry you must enter a valid operator')
    }};
over 4 years ago · Santiago Trujillo Relatório

0

Try removing the condition from swtich case

handelAssign = (operator) => {
    /**operatior could be + - * ÷ */
    switch (operator) {
      case "+":
        this.setState({ operator: "+" });
        break;
      case "-":
        this.setState({ operator: "-" });
        break;
      case "*":
        this.setState({ operator: "*" });
        break;
      case "÷":
        this.setState({ operator: "/" });
        break;
      default: 
        throw new Error('sorry you must enter a valid operator')
    }};
over 4 years ago · Santiago Trujillo 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