# 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.

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));``````

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);
``````

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)
)``````

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(' ')));``````

## Twitter typeahead - why does it only match one result?

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

## Strange behavior of React setState with let variable change

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 ="....

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