Home > database >  Combination inside switch case
Combination inside switch case

Time:06-04

I used to have a simple switch case, like this:

const expr = 'Papayas';
switch (expr) {
  case 'Oranges':
    console.log('Oranges');
    break;
  case 'Mangoes':
    console.log('Mangoes');
    break;
  case 'Papayas':
    console.log('Papayas');
    break;
}

but now some criteria changed and I need to add one rule for all three cases and leave the old functionality as it was. So in case, I'll have Papayas, my resalt would be All and Papayas. I tried to combine it like this:

const expr = 'Papayas';
switch (expr) {
  case 'Oranges':
  case 'Mangoes':
  case 'Papayas':
    console.log('All');
  case 'Oranges':
    console.log('Oranges');
    break;
  case 'Mangoes':
    console.log('Mangoes');
    break;
  case 'Papayas':
    console.log('Papayas');
    break;
}

but that doesn't work correctly. I always get All and Oranges in the result. Is there a way to do it with one switch case?

CodePudding user response:

You can make something like this:

const output = ['All'];

switch (expr) {
  case 'Oranges':
    output.push('Oranges');
    break;
  case 'Mangoes':
    output.push('Mangoes');
    break;
  case 'Papayas':
    output.push('Papayas');
    break;
}

console.log(...output);

CodePudding user response:

What happens if I forgot a break?

If you forget a break then the script will run from the case where the criterion is met and will run the cases after that regardless if a criterion was met. - the MDN JS docs on switch

What happens in your case is that it matches the case 'Papayas': console.log('All') and then runs the next case 'Oranges': console.log('Oranges') break; despite it not being met; afterwards comes the break of that case - without this you'd also be outputting Mangoes and Papayas.

The first, trivial fix is to just duplicate the console.log('All') in all switch cases:

switch (expr) {
  case 'Oranges': console.log('All'); console.log('Oranges'); break;
  case 'Mangoes': console.log('All'); console.log('Mangoes'); break;
  case 'Papayas': console.log('All'); console.log('Papayas');
}

the last break may be omitted. An alternative that requires the use of a second switch and duplicating the cases is to just execute one switch after another:

let val = expr;

switch (val) {
  case 'Oranges':
  case 'Mangoes':
  case 'Papayas':
    console.log('All')
}

switch (val) {
  case 'Oranges': console.log('Oranges'); break;
  case 'Mangoes': console.log('Mangoes'); break;
  case 'Papayas': console.log('Papayas'); break;
}

but I'd question whether a switch is the appropriate language construct to use here. An if comes to mind:

let val = expr;
if (val == 'Oranges' || val == 'Mangoes' || val == 'Papayas') {
  console.log('All');
  switch (val) {
    case 'Oranges': console.log('Oranges'); break;
    case 'Mangoes': console.log('Mangoes'); break;
    case 'Papayas': console.log('Papayas'); break;
  }
}

Ideally, you can look up the message in a dictionary / "object" in JS terminology:

const messages = {
  Oranges: 'Oranges',
  Mangoes: 'Mangoes',
  Papayas: 'Papayas'
}
let message = messages[expr];
if (message) {
  console.log('All');
  console.log(message);
}

Alternatively using arrow funcs if your code doesn't consist just of a mapping data -> data but rather data -> action:

const funcs = {
  Oranges: () => console.log('Oranges'),
  Mangoes: () => console.log('Mangoes'),
  Papayas: () => console.log('Papayas')
}
let func = funcs[expr];
if (func) {
  console.log('All');
  func();
}

CodePudding user response:

You could take advantage of the way select statements will continue after matching the first item if a break; isn't declared. Example below uses conditional breaks to output the following.

  • Input: 'All' = All - Oranges Mangoes and Papayas
  • Input: 'Oranges' = Oranges
  • Input: 'MangPap' = All - Mangoes and Papayas
  • Input: 'Mangoes' = Mangoes
  • Input: 'Papayas' = Papayas
  • Input: unrecognized (default) = Input not recognized.

const expr = 'MangPap';
var output = '';
switch (expr) {
  case 'All':
    output = 'All - ';
  case 'Oranges':
    if ( output.length > 0 ) { output  = 'Oranges'; } else { output  = 'Oranges'; }
    if (expr === 'Oranges') { break; }
  case 'MangPap':
    if ( output.length > 0 ) {  } else { output  = 'All - '; }
  case 'Mangoes':
    if ( output.length > 0 ) { output  = ' Mangoes'; } else { output  = 'Mangoes'; }
    if (expr === 'Mangoes') { break; }
  case 'Papayas':
    if ( output.length > 0 ) { output  = ' and Papayas'; } else { output  = 'Papayas'; }
    if (expr === 'Papayas') { break; }
  default:
    if ( output.length > 0 ) {  } else { output  = output = 'Input not recognized.'; }
}
console.log( output );

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

  • Related