Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: Handling multiple results from combinations Date: Tue, 24 Jan 2017 14:22:27 +0000 Organization: A noiseless patient Spider Lines: 39 Message-ID: <87lgu0tu58.fsf@bsb.me.uk> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="28da054d5599cafe2fbd69dbf74ef564"; logging-data="6009"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+sXEGJ0Nh+AMmHhTLpYySTfwXxB3UiLyE=" Cancel-Lock: sha1:+V3KQk86vHgTFyIZgUAY8ORxfp0= sha1:jQP18k+UL5S9ObAyr0SB5r2LdC0= X-BSB-Auth: 1.12cfb0d5fe84e4609a79.20170124142227GMT.87lgu0tu58.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:32297 Andrew Poulos writes: > Say there are 10 options that can be selected in any order and each > combination produces a different result. Is there some way to approach > judging the options to choose the result without having such a tree > full of IF statements: > > if (a && b && c...) { > // > } else if (a && b && !c...) { > // > } else if (a && !b && !c...) { > // > } else if (!a && b && !c...) { > // > } else if (!a && !b && !c...) { > ... > > Currently I'm working on an elearning question where there are > multiple options and different combinations of selected options gives > different feedback. A couple of extra points... It's probably better to have the answers in an array rather than in named variables. You don't need to use a numeric value based on 0/1 bit to generate a key for the response. You can also use a string. For example var response = [ true, false, false, true, false, true ]; response.map((r, i) => r ? i.toString()+'/' : '').join(''); will give '0/3/5/'. If you choose the right string, it might be easier when associating the result with the responses. -- Ben.