JavaScript Recursive for For loop recursive loop python,recursive loop detected in template string,recursive

I have a scenario to get a output like the following.

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

When the input is 3 it would give 8 combinations as above. If the input is 4, there would be 16 combinations. I am able to achieve this using nested for loop as below.

value1 = 2, value2 = 2, value3 = 2;
my function () {
    for(var i = 0; i<this.value1; i++) { 
      for(var j = 0; j < this.value2; j++) { 
        for(var k = 0; k < this.value3; k++) { 
           console.log(i,j,k);
        }
      }
    }
}

This is fine if it is for input 3, But I have to get the output for input 10. So I need to write 10 nested for loops as above. I know it is bad practice, I don't know how to use the recursive here. Please help me on this.

Answer:1

Here is a recursive function that can do the required job.

function solveUtil(limits, index, cur) {
    if (index === limits.length) {
        console.log(cur.join(' '));
    } else {

        for (let i = 0; i < limits[index]; i++) {
            cur.push(i);
            solveUtil(limits, index + 1, cur);
            cur.pop();
        }
    }
}

function solve(limits) {
    solveUtil(limits, 0, []);
}

solve(new Array(10).fill(2));
Answer:2

The recursive approach would look something like this:

function recursive(n, range, prefix) {
  prefix = prefix || "";
  if (n == 0)
    console.log(prefix);
  else
    for (let i = 0; i < range; i++)
      recursive(n - 1, range, prefix + " " + i);
}

recursive(10, 2);
Answer:3

You could take advantage of the fact you're looking for combinations in binary and just simply count from 0 in binary and add padding.

This uses function generators, but it's easy to convert it to a simple function.

function * findCombinations(n){
  for(let i = 0; i < Math.pow(2, n); i++){
    const c = i.toString(2);
    const padding = new Array(n - c.length + 1).join('0');
    yield padding + c;
  }
  return null;
}

const iterator = findCombinations(4);
console.log(
  Array.from(iterator)
)
Answer:4

In newer browsers (which support .padStart), the following produces the desired output (but bypasses rather than solves the problem with recursion):

let n = 4; //or any other value
    
Array(Math.pow(2,n)).fill(0).forEach((_,i) => console.log(i.toString(2).padStart(n,'0').split('').join(' ')));
Answer:5

I'm using the Twitter Typeahead plugin: https://twitter.github.io/typeahead.js/ Here is the code I'm using: $(document).ready( function() { var bestPictures = new Bloodhound({ datumTokenizer:...

I'm using the Twitter Typeahead plugin: https://twitter.github.io/typeahead.js/ Here is the code I'm using: $(document).ready( function() { var bestPictures = new Bloodhound({ datumTokenizer:...

The code below will render a <h1> and two <button>. In my expectation, the changeString1 <button> will change letString to 1 and change <h1> text to 1 finally, while the ...

The code below will render a <h1> and two <button>. In my expectation, the changeString1 <button> will change letString to 1 and change <h1> text to 1 finally, while the ...

I have two domains. [www.one.de and www.two.de] on www.one.de I have a script witch makes a ajaxcall to www.two.de/collect.php First I call on www.two.de this php-script to set a cookie: $domain ="....

I have two domains. [www.one.de and www.two.de] on www.one.de I have a script witch makes a ajaxcall to www.two.de/collect.php First I call on www.two.de this php-script to set a cookie: $domain ="....

  1. access cookies
  2. access cookies javascript
  3. access cookie from different domain
  4. access cookie react
  5. access cookie from server
  6. access cookie from iframe
  7. access cookies on iphone
  8. access cookies on android
  9. access cookie in php
  10. access cookies in angular
  11. access cookie from subdomain
  12. access cookies in jquery
  13. access cookies express
  14. access cookies in html
  15. access cookies on ipad
  16. access cookies from chrome extension
  17. access cookie in jsp
  18. access cookies from service worker
  19. access cookies from another domain
  20. access cookies in c#

I was trying the below commands on Chrome console. I am able to create the object using new(line 2 below) but using call doesn't work. Can anyone explain what could be the reason ? function ...

I was trying the below commands on Chrome console. I am able to create the object using new(line 2 below) but using call doesn't work. Can anyone explain what could be the reason ? function ...