# 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');
}
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

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

3. access cookie from different domain