Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #32287 > unrolled thread

Handling multiple results from combinations

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2017-01-24 14:44 +1100
Last post2017-01-26 23:21 +0100
Articles 18 — 7 participants

Back to article view | Back to comp.lang.javascript


Contents

  Handling multiple results from combinations Andrew Poulos <ap_prog@hotmail.com> - 2017-01-24 14:44 +1100
    Re: Handling multiple results from combinations Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-24 05:34 +0100
      Re: Handling multiple results from combinations Andrew Poulos <ap_prog@hotmail.com> - 2017-01-24 16:54 +1100
        Re: Handling multiple results from combinations Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-24 07:14 +0100
    Re: Handling multiple results from combinations "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-23 22:31 -0800
      Re: Handling multiple results from combinations "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-01-24 11:27 +0100
        Re: Handling multiple results from combinations "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-24 16:41 -0800
          Re: Handling multiple results from combinations "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-01-25 11:17 +0100
            Re: Handling multiple results from combinations "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-25 05:19 -0800
              Re: Handling multiple results from combinations "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-25 06:25 -0800
              Re: Handling multiple results from combinations "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-01-25 17:37 +0100
                Re: Handling multiple results from combinations "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-25 08:47 -0800
    Re: Handling multiple results from combinations Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-24 14:22 +0000
    Re: Handling multiple results from combinations Doc O'Leary  <droleary@2015usenet1.subsume.com> - 2017-01-24 20:17 +0000
      Re: Handling multiple results from combinations "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-24 16:47 -0800
    Re: Handling multiple results from combinations Dr J R Stockton <reply1700@merlyn.demon.co.uk.invalid> - 2017-01-25 22:45 +0000
      Re: Handling multiple results from combinations "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-25 17:53 -0800
        Re: Handling multiple results from combinations Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-26 23:21 +0100

#32287 — Handling multiple results from combinations

FromAndrew Poulos <ap_prog@hotmail.com>
Date2017-01-24 14:44 +1100
SubjectHandling multiple results from combinations
Message-ID<B9OdnUTUM9EuUBvFnZ2dnUU7-dnNnZ2d@westnet.com.au>
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. There isn't 10! possible feedback but enough to make the 
building and testing of the question slow going.

Andrew Poulos

[toc] | [next] | [standalone]


#32290

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-01-24 05:34 +0100
Message-ID<3395715.fW5hKsROvD@PointedEars.de>
In reply to#32287
Andrew Poulos wrote:

> 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...) {
> ...

Yes.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#32292

FromAndrew Poulos <ap_prog@hotmail.com>
Date2017-01-24 16:54 +1100
Message-ID<1OudnbYCL-NidhvFnZ2dnUU7-KWdnZ2d@westnet.com.au>
In reply to#32290
On 24/01/2017 3:34 PM, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
>
>> 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...) {
>> ...
>
> Yes.

If you're unwilling to tell me what the approach is please direct me to 
where I can find out.

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#32293

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-01-24 07:14 +0100
Message-ID<1537383.vCJZsxu672@PointedEars.de>
In reply to#32292
Andrew Poulos wrote:

> If you're unwilling to tell me what the approach is please direct me to
> where I can find out.

ACK

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#32294

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2017-01-23 22:31 -0800
Message-ID<ffb15942-c460-4fad-8164-7b504ee1b4df@googlegroups.com>
In reply to#32287
On Monday, January 23, 2017 at 9:45:04 PM UTC-6, Andrew Poulos wrote:
> 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. There isn't 10! possible feedback but enough to make the 
> building and testing of the question slow going.

Combinations or permutations? Does the order matter? One thought is to build some tables to make sure you covered your paths:

    A B C D E F G H I J
------------------------
A |
B |
C |
D |
E |
F |
G |
H |
I |
J |

     C D E F G H I J
-----------------------
AB |
AC |
AD |
AE |
AF |
AG |
AH |
AI |
AJ |


     D E F G H I J
--------------------
ABC|
ACD|
ADE|
AEF|
AFG|
AGH|
AHI|
AIJ|

     E F G H I J
--------------------
ABCD|
ACDE|
ADEF|
AEFG|
AFGH|
AGHI|
AHIJ|

etc.

Since these are all ON|OFF choices (I assume), you can represent the chosen options as 1|0

Therefore you could then use a 10 variable Karnaugh map [1] and simplify your logic [2] to what matters and what is redundant (hence fewer conditionals)

[1] <https://en.wikipedia.org/wiki/Karnaugh_map>
[2] <https://www.youtube.com/watch?v=TykWFoQPE_s>

[toc] | [prev] | [next] | [standalone]


#32295

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-01-24 11:27 +0100
Message-ID<XnsA707747F1A2D9eejj99@194.109.6.166>
In reply to#32294
"Michael Haufe (TNO)" <tno@thenewobjective.com> wrote on 24 Jan 2017 in
comp.lang.javascript: 

> On Monday, January 23, 2017 at 9:45:04 PM UTC-6, Andrew Poulos wrote:
>> 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. There isn't 10! possible feedback but enough to make the 
>> building and testing of the question slow going.
> 
> Combinations or permutations? Does the order matter? One thought is to
> build some tables to make sure you covered your paths: 
> 

>"Michael Haufe (TNO)" <tno@thenewobjective.com> wrote:
> > [..] 

> > Therefore you could then use a 10 variable Karnaugh map 

Okay, so in JS:

<script type='text/javascript'> 'use strict'

// true/false list for a [5 variable] Karnaugh map
let a = 1;
let b = 1;
let c = 1;
let d = 0;
let e = 0;

// Karnaugh resultarray 2*5 = 32 
// members may be any [executable?] text 
let k = ['r0','r1','r2','r3',
	'r4','r5','r6','r7',
	'r8','r9','r10','r11',
	'r12','r13','r14','r15' 
	// etc till 'r31'
	];

let temp = 1*a + 2*b + 4*c + 8*d + 16*e;

// the result can also be evilly eval()-ed:
alert('Result: ' + k[temp] );

</script>




-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#32320

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2017-01-24 16:41 -0800
Message-ID<067006f4-6190-416d-a1e6-25b4a718b4b4@googlegroups.com>
In reply to#32295
Evertjan. wrote:

> Okay, so in JS:
> 
> <script type='text/javascript'> 'use strict'
> 
> // true/false list for a [5 variable] Karnaugh map
> let a = 1;
> let b = 1;
> let c = 1;
> let d = 0;
> let e = 0;
> 
> // Karnaugh resultarray 2*5 = 32 
> // members may be any [executable?] text 
> let k = ['r0','r1','r2','r3',
> 	'r4','r5','r6','r7',
> 	'r8','r9','r10','r11',
> 	'r12','r13','r14','r15' 
> 	// etc till 'r31'
> 	];
> 
> let temp = 1*a + 2*b + 4*c + 8*d + 16*e;
> 
> // the result can also be evilly eval()-ed:
> alert('Result: ' + k[temp] );
> 
> </script>

I think you misunderstand the purpose of a K-Map. It's for simplification of compound Boolean expressions. The potential application for the OP would be to reduce the number of if-else statements. IIUC, you're thinking of this in terms of a degenerate Incidence matrix [1]

[1] <https://en.wikipedia.org/wiki/Incidence_matrix>

[toc] | [prev] | [next] | [standalone]


#32332

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-01-25 11:17 +0100
Message-ID<XnsA70872D131AFFeejj99@194.109.6.166>
In reply to#32320
"Michael Haufe (TNO)" <tno@thenewobjective.com> wrote on 25 Jan 2017 in
comp.lang.javascript: 

> Evertjan. wrote:
> 
>> Okay, so in JS:
>> 
>> <script type='text/javascript'> 'use strict'
>> 
>> // true/false list for a [5 variable] Karnaugh map
>> let a = 1;
>> let b = 1;
>> let c = 1;
>> let d = 0;
>> let e = 0;
>> 
>> // Karnaugh resultarray 2*5 = 32 
>> // members may be any [executable?] text 
>> let k = ['r0','r1','r2','r3',
>>      'r4','r5','r6','r7',
>>      'r8','r9','r10','r11',
>>      'r12','r13','r14','r15' 
>>      // etc till 'r31'
>>      ];
>> 
>> let temp = 1*a + 2*b + 4*c + 8*d + 16*e;
>> 
>> // the result can also be evilly eval()-ed:
>> alert('Result: ' + k[temp] );
>> 
>> </script>
> 
> I think you misunderstand the purpose of a K-Map. It's for
> simplification of compound Boolean expressions. 

You might certainly think so, 
but this is not about the PURPOSE of a K-Map,
which is not quite part of its definition.

This is about the solution of ...

> The potential
> application for the OP would be to reduce the number of if-else
> statements. 

indeed, which my example posed.

> IIUC, you're thinking of this in terms of a degenerate
> Incidence matrix [1] 

You might certainly think so, 
but I think I am in a less degenerate position to 
grasp what I think.

> [1] <https://en.wikipedia.org/wiki/Incidence_matrix>

I reduced the number of if-else statements to ZERO.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#32337

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2017-01-25 05:19 -0800
Message-ID<bb42617c-4a1e-4679-a6a2-07ed3fe5c312@googlegroups.com>
In reply to#32332
Evertjan. wrote:
 
> You might certainly think so, 
> but I think I am in a less degenerate position to 
> grasp what I think.

I'm referring to a different definition of "degenerate"; namely #4 of <https://en.wiktionary.org/wiki/degenerate#Adjective>, but feel free to feel insulted anyway.

[toc] | [prev] | [next] | [standalone]


#32338

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2017-01-25 06:25 -0800
Message-ID<96e06ad8-fe50-4d6a-af25-04fcdcc0142f@googlegroups.com>
In reply to#32337
Michael Haufe (TNO) wrote:
> Evertjan. wrote:
>  
> > You might certainly think so, 
> > but I think I am in a less degenerate position to 
> > grasp what I think.
> 
> I'm referring to a different definition of "degenerate"; namely #4 of <https://en.wiktionary.org/wiki/degenerate#Adjective>, but feel free to feel insulted anyway.

...and regardless, can you show an example usage of your solution given my questions were a set of colors?


const RED     = 0xFF0000
const ORANGE  = 0xFFA500
const YELLOW  = 0xFFFF00
const GREEN   = 0x00FF00
const BLUE    = 0x0000FF
const INDIGO  = 0x4B0082
const VIOLET  = 0xEE82EE
const WHITE   = 0xFFFFFF
const BLACK   = 0x000000
const FUCHSIA = 0xFF00FF

var chosenColor = RED | BLUE

[toc] | [prev] | [next] | [standalone]


#32342

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-01-25 17:37 +0100
Message-ID<XnsA708B34875876eejj99@194.109.6.166>
In reply to#32337
"Michael Haufe (TNO)" <tno@thenewobjective.com> wrote on 25 Jan 2017 in
comp.lang.javascript: 

> Evertjan. wrote:
>  
>> You might certainly think so, 
>> but I think I am in a less degenerate position to 
>> grasp what I think.
> 
> I'm referring to a different definition of "degenerate"; namely #4 of
> <https://en.wiktionary.org/wiki/degenerate#Adjective>, but feel free to
> feel insulted anyway. 

You might certainly think so,
but I NEVER feel insulted,
though I don't feel unfree to do so.

In fact,
I think feeling insulted is silly in itself and also a waste of time.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#32344

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2017-01-25 08:47 -0800
Message-ID<0aefb34d-51ba-484a-8fc8-227daad890ec@googlegroups.com>
In reply to#32342
Evertjan. wrote:
> "Michael Haufe (TNO) wrote

> > I'm referring to a different definition of "degenerate"; namely #4 of
> > <https://en.wiktionary.org/wiki/degenerate#Adjective>, but feel free to
> > feel insulted anyway. 
> 
> You might certainly think so,
> but I NEVER feel insulted,
> though I don't feel unfree to do so.
> 
> In fact,
> I think feeling insulted is silly in itself and also a waste of time.

fair enough

[toc] | [prev] | [next] | [standalone]


#32297

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2017-01-24 14:22 +0000
Message-ID<87lgu0tu58.fsf@bsb.me.uk>
In reply to#32287
Andrew Poulos <ap_prog@hotmail.com> 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.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#32308

FromDoc O'Leary <droleary@2015usenet1.subsume.com>
Date2017-01-24 20:17 +0000
Message-ID<o68cp0$vok$1@dont-email.me>
In reply to#32287
For your reference, records indicate that 
Andrew Poulos <ap_prog@hotmail.com> wrote:

> 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:

<https://en.wikipedia.org/wiki/Finite-state_machine>

Bonus points if you use polymorphism to implement it in an OO 
language like JavaScript.

-- 
"Also . . . I can kill you with my brain."
River Tam, Trash, Firefly

[toc] | [prev] | [next] | [standalone]


#32321

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2017-01-24 16:47 -0800
Message-ID<d3917aaf-15a7-4217-a350-c5b9f78f19de@googlegroups.com>
In reply to#32308
Doc O'Leary wrote:
> For your reference, records indicate that 
> Andrew Poulos wrote:
> 
> > 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:
> 
> <https://en.wikipedia.org/wiki/Finite-state_machine>
> 
> Bonus points if you use polymorphism to implement it in an OO 
> language like JavaScript.

Where the State is the answer and Transition is the option?


var ANSWER_1 = State(),
    ANSWER_2 = State(),
    ...

ANSWER_1(CHOSEN_OPT_1)(() => ANSWER_2)
ANSWER_1(CHOSEN_OPT_3)(() => ANSWER_1)

ANSWER_2(CHOSEN_OPT_4)(() => ANSWER_3)

...

[toc] | [prev] | [next] | [standalone]


#32348

FromDr J R Stockton <reply1700@merlyn.demon.co.uk.invalid>
Date2017-01-25 22:45 +0000
Message-ID<Beu5S02dqSiYFwq1@invalid.uk.co.demon.merlyn.invalid>
In reply to#32287
In comp.lang.javascript message <B9OdnUTUM9EuUBvFnZ2dnUU7-dnNnZ2d@westne
t.com.au>, Tue, 24 Jan 2017 14:44:52, Andrew Poulos
<ap_prog@hotmail.com> posted:

>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. There isn't 10! possible feedback but enough to make the
>building and testing of the question slow going.


You could maybe concatenate the booleans in a string as illustrated by
        var Q = "F" + +(1==1) + +(1==2) + +(1!=2)
then write functions for each case - function F101() { ... } for that -
and then merely execute   window[Q]()   // !!!     untested.  Lo, not an
"if" in sight.

-- 
 (c) John Stockton, Surrey, UK.  ¬@merlyn.demon.co.uk   Turnpike v6.05   MIME.
 Merlyn Web Site <                       > - FAQish topics, acronyms, & links.

[toc] | [prev] | [next] | [standalone]


#32351

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2017-01-25 17:53 -0800
Message-ID<874e7957-7f2f-48f1-8083-e3b653f46797@googlegroups.com>
In reply to#32348
Dr J R Stockton wrote:

> You could maybe concatenate the booleans in a string as illustrated by
>         var Q = "F" + +(1==1) + +(1==2) + +(1!=2)
> then write functions for each case - function F101() { ... } for that -
> and then merely execute   window[Q]()   // !!!     untested.  Lo, not an
> "if" in sight.

Might as well use a Bit Mask then [1] ...

[1] <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Flags_and_bitmasks>

[toc] | [prev] | [next] | [standalone]


#32361

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-01-26 23:21 +0100
Message-ID<1771290.oMNUckLgyt@PointedEars.de>
In reply to#32351
Michael Haufe (TNO) wrote:

> Dr J R Stockton wrote:
>> You could maybe concatenate the booleans in a string as illustrated by
>>         var Q = "F" + +(1==1) + +(1==2) + +(1!=2)
>> then write functions for each case - function F101() { ... } for that -
>> and then merely execute   window[Q]()   // !!!     untested.  Lo, not an
>> "if" in sight.
> 
> Might as well use a Bit Mask then [1] ...

A bit mask is something slightly different.  A bit _Map_ (sic) and bitwise 
operations is what I would use as a start, with names for the bits to keep 
it easily maintainable.  However, Ben’s string approach is interesting too, 
as it has the advantage over bitwise operations that it works for more than 
32 selectors¹, basically an infinite number of them (same principle as 
arbitrary-precision arithmetic).

_______
¹  This is not obvious from what you referred to: Bitwise operations are 
   limited to 32-bit integers (ES 2016, § 12.13), so even though you can
   store integers precisely natively up to including 2⁵³ (ES 2016,
   § 20.1.2.6), you cannot reasonably use all of those in bitwise
   operations.

   Apropos powers of 2, since last year (2016) ECMAScript has an 
   exponentiation operator: 2 ** 53 === Math.pow(2, 53), specified in
   ES 2016, § 20.2.2.26 and 12.7.3.4.  I found that out by accident in the
   DevTools console this week when testing possible solutions and being
   surprised that the typo “**” did not result in a SyntaxError exception.
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web