4

I want to create an array of all possible combinations of three variables that can either be true or false (i.e. 8 possible combinations).

I am trying to create the cube in the top left corner at this image

enter image description here

So the output should be something like

points = [
  // first square
  {
    id: '000',
    truths: [false, false, false]
    position: [0, 0]
  },
  {
    id: '100',
    truths: [true, false, false]
    position: [5, 0]
  },
  {
    id: '010',
    truths: [false, true, false]
    position: [0, 5]
  },
  {
    id: '110',
    truths: [true, true, false]
    position: [5, 5]
  },
  // second square
  {
    id: '001',
    truths: [false, false, true]
    position: [2.5, 2.5]
  },
  {
    id: '101',
    truths: [true, false, true]
    position: [7.5, 2.5]
  },
  {
    id: '011',
    truths: [false, true, true]
    position: [2.5, 7.5]
  },
  {
    id: '111',
    truths: [true, true, true]
    position: [7.5, 7.5]
  },
];

lines = [
  { from: '000', to: '100' },
  { from: '000', to: '010' },
  { from: '000', to: '001' },

  { from: '100', to: '101' },
  { from: '100', to: '110' },

  { from: '001', to: '101' },
  { from: '001', to: '011' },

  { from: '101', to: '001' },
  { from: '101', to: '111' },

  ...
]

I don't know how to go through all possible truth values and create these points.

One approach could be to use a for loop

for (var i=0; i<Math.pow(2, 3); i++) {
  ...
}

but it doesn't help me assigning the possible truth values.

mortensen
  • 1,167
  • 2
  • 13
  • 23
  • 1
    There are 2^n possible values. If you don't want to use nested for loops (you really shouldn't) then extract the bits of the integers `0...2^n`. The `n` values in `truths` will be the bits of the integer. – plasmacel Sep 27 '16 at 21:56
  • I just don't get if your order is 0,4,2,3,1,5,7,8 how a binary approach will help you. Why don't you use just numbers. – Redu Sep 27 '16 at 22:11
  • @Redu I don't get what are you talking about. The order doesn't matter. All integers from 0 to 8 will represent 3 bits, which correspond to the `truths` array in the OP's analogy. 2^n integers = 2^n `truths` arrays. In binary, numbers can be thought as "arrays" of bits: 0=[0,0,0], 1=[0,0,1], 2=[0,1,0], 3=[0,1,1], 4=[1,0,0] , 5=[1,0,1], 6=[1,1,0], 7=[1,1,1]. – plasmacel Sep 27 '16 at 22:35

5 Answers5

10

Everything in a computer is already binary. You don't need any fancy Math.pow or similar.

for (let i = 0; i < 1 << 3; i++) {
  console.log([!!(i & (1<<2)), !!(i & (1<<1)), !!(i & 1)]);
}

While this looks nice and short, i am actually not a fan of !! or magic numbers. I always fall for these tricks when writing snippets though. Therefore will attempt to give a slightly cleaner version:

const AMOUNT_OF_VARIABLES = 3;

for (let i = 0; i < (1 << AMOUNT_OF_VARIABLES); i++) {
  let boolArr = [];
  
  //Increasing or decreasing depending on which direction
  //you want your array to represent the binary number
  for (let j = AMOUNT_OF_VARIABLES - 1; j >= 0; j--) {
    boolArr.push(Boolean(i & (1 << j)));
  }
  
  console.log(boolArr);
}
ASDFGerte
  • 4,695
  • 6
  • 16
  • 33
  • Hi, just asking because of curiosity... does that `0` (`for let i = 0;`) equal a binary `0`? Is it the same as `1 >>> 1`? – Alejandro Iván Sep 27 '16 at 22:03
  • `0` is really zero. Just look at it this way, javascript binary operators work "as if" on 32bit integers, we are only using three bits: 000, 001, 010, 011, 100, 101, 111. Just omitting the first 29 zeroes, we don't care about those. – ASDFGerte Sep 27 '16 at 22:04
  • Ok, thanks for your clarification! I'm used to C-like stuff (where I usually use `\0`), so sometimes I get confused. – Alejandro Iván Sep 27 '16 at 22:05
  • 1
    @AlejandroIván but note that `1 >>> 1 === 0` – ASDFGerte Sep 27 '16 at 22:09
2

It's easy, just convert all integer from 0 through 2**n-1 to binary:

var n = 3,
    m = 1 << n;
for (var i = 0; i < m; i++) {
    var s = i.toString(2); // convert to binary
    s = new Array(n + 1 - s.length).join('0') + s; // pad with zeroes
    console.log(s);
}

The above code is general; you can change n to the number bits that you want.

redneb
  • 21,794
  • 6
  • 42
  • 54
2

There are pow(2, n) possible values.

In the binary number system, numbers can be simply thought as "arrays" of bits: 0=[0,0,0], 1=[0,0,1], 2=[0,1,0], 3=[0,1,1], 4=[1,0,0], 5=[1,0,1], 6=[1,1,0], 7=[1,1,1].

Following this idea, the simplest approach is to extract the bits of the integers [0, pow(2, n) - 1]. Here is the code which is a straightforward implementation of the above idea:

function test()
{
   var n = 3;
   var k = (1 << n); // bit trick for pow(2, n)

   var truths = [];

   for (var i = 0; i < k; ++i)
   {
      truths[i] = [];

      for (var j = 0; j < n; ++j)
      {
         var value = (i >> j) & 1; // extract the j-th bit of i
         truths[i][j] = value;
      }

      console.log(truths[i]);
   }
}
plasmacel
  • 8,183
  • 7
  • 53
  • 101
1

const boolCombo = size => {
  const buf = Array(1 << size)
  for (let i = buf.length; i--;) {
    buf[ i ] = Array(size)
    for (let j = size; j--;)
      buf[ i ][ j ] = +!!(i & 1 << j)
  }
  return buf
}

console.log(boolCombo(3))
-1

#include <stdio.h>

#include <math.h>

int main()

{

int n, c, k,arr[100],m,i;

scanf("%d", &n);

m=pow(2,n);

for(i=0;i<m;i++)

arr[i]=i;

for(i=0;i<m;i++)

{

for (c = n-1; c >= 0; c--)

{

k = arr[i] >> c;

if (k & 1)

printf("true ");

else

printf("false ");

}

printf("\n");

}

return 0;

}

  • Welcome to StackOverflow. Please check [how to answer questions](https://stackoverflow.com/help/how-to-answer) – CyberEternal Aug 28 '21 at 20:50
  • Please add further details to expand on your answer, such as working code or documentation citations. – Community Aug 28 '21 at 20:51
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Sep 17 '21 at 10:37
  • https://stackoverflow.com/editing-help – Yunnosch Sep 17 '21 at 10:37