How to generate all possible outcome for if statement?

Viewed 88

So I have this if statement and I want to make all the possible outcomes.

if (cuisine == '' && area == '' && city == '' && minprice == '' && maxprice == ''){}
if (cuisine != '' && area == '' && city == '' && minprice == '' && maxprice == ''){}
if (cuisine != '' && area != '' && city == '' && minprice == '' && maxprice == ''){}
if (cuisine != '' && area != '' && city != '' && minprice == '' && maxprice == ''){}
if (cuisine != '' && area != '' && city != '' && minprice != '' && maxprice == ''){}
if (cuisine != '' && area != '' && city != '' && minprice != '' && maxprice != ''){}

etc... etc...

2 Answers

Here is a coding version of the truth table

const conditions = {
  "cuisine": '',
  "area": '',
  "city": '',
  "minprice": '',
  "maxprice": ''
}
const arr = Object.keys(conditions),
 n = arr.length,
  m = 1 << n,
table = Array.from({length: m}).map((_,i) => {
  let s = i.toString(2); // convert to binary
  let len = n + 1 - s.length
  s = Array.from({length:len}).join('0') + s; // pad with zeroes
  return `if (${s.split('').map((bit,j) => `${arr[j]}${bit==="1"?' == ':' != '}''`).join(' && ')}) {...}`;
})

document.querySelector('pre').innerHTML = `     ${table.sort().join('\nelse ')}`
<pre></pre>

These are the all the possible outcomes

              if(cuisine == '' && area == '' && city == '' && minprice == '' && maxprice == ''){}
        else if (cuisine == '' && area == '' && city == '' && minprice == '' && maxprice !== ''){}
        else if (cuisine == '' && area == '' && city == '' && minprice !== '' && maxprice == ''){}
        else if (cuisine == '' && area == '' && city == '' && minprice !== '' && maxprice !== ''){}
        else if (cuisine == '' && area == '' && city !== '' && minprice == '' && maxprice == ''){}
        else if (cuisine == '' && area == '' && city !== '' && minprice == '' && maxprice !== ''){}
        else if (cuisine == '' && area == '' && city !== '' && minprice !== '' && maxprice == ''){}
        else if (cuisine == '' && area == '' && city !== '' && minprice !== '' && maxprice !== ''){}
        else if (cuisine == '' && area !== '' && city == '' && minprice == '' && maxprice == ''){}
        else if (cuisine == '' && area !== '' && city == '' && minprice == '' && maxprice !== ''){}
        else if (cuisine == '' && area !== '' && city == '' && minprice !== '' && maxprice == ''){}
        else if (cuisine == '' && area !== '' && city == '' && minprice !== '' && maxprice !== ''){}
        else if (cuisine == '' && area !== '' && city !== '' && minprice == '' && maxprice == ''){}
        else if (cuisine == '' && area !== '' && city !== '' && minprice == '' && maxprice !== ''){}
        else if (cuisine == '' && area !== '' && city !== '' && minprice !== '' && maxprice == ''){}
        else if (cuisine == '' && area !== '' && city !== '' && minprice !== '' && maxprice !== ''){}
        else if (cuisine !== '' && area == '' && city == '' && minprice == '' && maxprice == ''){}
        else if (cuisine !== '' && area == '' && city == '' && minprice == '' && maxprice !== ''){}
        else if (cuisine !== '' && area == '' && city == '' && minprice !== '' && maxprice == ''){}
        else if (cuisine !== '' && area == '' && city == '' && minprice !== '' && maxprice !== ''){}
        else if (cuisine !== '' && area == '' && city !== '' && minprice == '' && maxprice == ''){}
        else if (cuisine !== '' && area == '' && city !== '' && minprice == '' && maxprice !== ''){}
        else if (cuisine !== '' && area == '' && city !== '' && minprice !== '' && maxprice == ''){}
        else if (cuisine !== '' && area == '' && city !== '' && minprice !== '' && maxprice !== ''){}
        else if (cuisine !== '' && area !== '' && city == '' && minprice == '' && maxprice == ''){}
        else if (cuisine !== '' && area !== '' && city == '' && minprice == '' && maxprice !== ''){}
        else if (cuisine !== '' && area !== '' && city == '' && minprice !== '' && maxprice == ''){}
        else if (cuisine !== '' && area !== '' && city == '' && minprice !== '' && maxprice !== ''){}
        else if (cuisine !== '' && area !== '' && city !== '' && minprice == '' && maxprice == ''){}
        else if (cuisine !== '' && area !== '' && city !== '' && minprice == '' && maxprice !== ''){}
        else if (cuisine !== '' && area !== '' && city !== '' && minprice !== '' && maxprice == ''){}
        else if (cuisine !== '' && area !== '' && city !== '' && minprice !== '' && maxprice !== ''){}

There are 5 varing things that have two possible outcomes(empty or not empty) Therefore there are 2x2x2x2x2 possibilities.

Related