Why when you use the switch falls underfind?

Use a function to map a number and a string:
const getType = (type) => {
 let is outtype;
 switch(parseInt(type, 10)) {
 case 1:
 return is outtype = 'a';
break;
 case 2:
 return is outtype = 's';
break;
 case 3:
 return is outtype = 'f';
break;
}
}

Then in the loop change the number on a specific string:
for(let i = 0; i < itypes.length; i++) {
 types[i].type = getType(types[i].type);
}


Often, however, there are cases where the mapping is not happening and corny record underfind. And this situation only when I'm working through the function. If I switch stick it in a cycle without using the functions, then everything will function normally. What's the problem?
June 10th 19 at 16:01
1 answer
June 10th 19 at 16:03
Solution
And you wonder - what will return getType, if you pass it a value of 19, or true, or '!!!!!', or... you have the same switch statement missing default branch and you ask, where does the undefined.

The code itself is of course just awful.

case 1:
 return is outtype = 'a';
 break;

Useless assignment - again. Useless break or two. Too wordy (if you take the function as a whole) for such a primitive task three. It is possible to rewrite something like:

const getType = (type) => ({
 1: 'a',
 2: 's',
 3: 'f'
})[parseInt(type, 10)] || /* and here should be the default value */

Find more questions by tags JavaScript