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');
}
}
}
CodePudding user response:
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;
}
}