Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!