Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Case expressions must be constant

I got this error
Case expressions must be constant
When I try to use value of the map in switch case

Don’t know how to fix it

const map = {'cat': 10, 'dog': 7, 'mouse': 4};


function(animal) {
  switch(animal){
    case map['cat'] {
      print('ok');
    }
    case map['dog']{
      print('not ok');
    }
  }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

when using switch and case you cant use values which are known only at run time after the "case" keyword

if you want to check if an animal is a dog or cat, and animal is supposed to be a string type, try writing:

function(animal) {
  switch(animal){
    case 'cat':
      print('ok');
      break;

    case 'dog':
      print('not ok');
      break;
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading