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

210
Visualizações
Is it possible to use !(not) in JS switch statement?

Actually, I was working with the JS switch statement. I used (!) in a switch statement, but it is not working. here's my code

function showResult(){
  let firstNum = document.getElementById("num1").value;
  firstNum = Number(firstNum);
  let secNum = document.getElementById("num2").value;
  secNum = Number(secNum);
  let operator = document.getElementById("operator").value;
  let getErrorElem = document.getElementById("error");
  let getResElem = document.getElementById("res");
  let result;
  // alert(typeof(operator));

  switch(operator){
      case null:
      case "":
          getErrorElem.innerHTML = "Operatot field can't be empty";
          break;
  }

  switch(!(operator)){
      case "+":
      case "-":
      case "/":
      case "*":
          alert("You can only input (+, -, *, /) these signs in operator field");
          break;
  }
}

I got three values from the user, the first number, the second number and the operator in the operator field I only want (+,-,*,/) these values, except these values there's an error message. I used a switch statement to validate this field, but not working. If I use (if) statement it is working properly. Please explain that how can I validate this field with switch statement using ! operator.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It seems you need something like this:

function showResult(){
  let firstNum = document.getElementById("num1").value;
  firstNum = Number(firstNum);
  let secNum = document.getElementById("num2").value;
  secNum = Number(secNum);
  let operator = document.getElementById("operator").value;
  let getErrorElem = document.getElementById("error");
  let getResElem = document.getElementById("res");
  let result;
  // alert(typeof(operator));

  switch(operator){
      case null:
      case "":
          getErrorElem.innerHTML = "Operatot field can't be empty";
          break;

      case "+":
      case "-":
      case "/":
      case "*":
          // Handle the right input.
          break;

      default:
          alert("You can only input (+, -, *, /) these signs in operator field");
          break;
  }
}
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