Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda