Home > Software design >  Check two variables in js switch case
Check two variables in js switch case

Time:12-06

I have 2 variables that return true and false. And I want each one that was false to return the corresponding text. How to handle this ValidCheked and repeatChecked in the switch?

   export const checked = (e, options) => {
    
      const nameCheked = new RegExp("^[a-zA-Z0-9]{1,50}$");
    
      const repeatChecked = options.every(
        (pool) => pool.text.toLowerCase() !== e.target.value.toLowerCase()
      );
      const validCheked = nameCheked.test(e.target.value);
    
      switch (repeatChecked) {
        case true:
          return {
            type: true,
            text: "looks good"
          };
        case false:
          return {
            type: false,
            text: "repeatName"
          }
        case validCheked = true:
          return {
            type: true,
            text: "looks good"
          }
        case validCheked = false:
          return {
            type: true,
            text: "Invalid name (no special character)"
          }
        default:
          return {
            type: false,
            text: "looks good"
          }
      }
    }

CodePudding user response:

You can use switch structure like this:

switch (true) {
 case (repeatChecked== true && validCheked == true):
   //do something (if you return something here, don't need to break;)
   break;
 case (repeatChecked== false && validCheked == false):
   //do something (if you return something here, don't need to break;)
   break;
  ...
}
  • Related