Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,003 questions

51,950 answers

573 users

How to generate all the binary options of N bits in Node.js

1 Answer

0 votes
function generateAllBinaryOptions(N) {
    let binary = [];

    const total_bits = parseInt("1".repeat(N), 2) + 1;

    for (let i = 0; i < total_bits; i++) {
        binary.push(i.toString(2).padStart(N, '0'));
    }

  return binary;
}


const N = 4;
console.log(generateAllBinaryOptions(N));




/*
run:

[
  '0000', '0001', '0010',
  '0011', '0100', '0101',
  '0110', '0111', '1000',
  '1001', '1010', '1011',
  '1100', '1101', '1110',
  '1111'
]

*/

 



answered Aug 26, 2023 by avibootz

Related questions

1 answer 108 views
1 answer 107 views
1 answer 103 views
1 answer 94 views
1 answer 101 views
1 answer 113 views
2 answers 142 views
...