# If condition breaks for loop in recursive function

**URL:** <https://discourse.processing.org/t/if-condition-breaks-for-loop-in-recursive-function/16717>\
**Category:** Coding Questions\
**Created:** [December 29, 2019, 9:49pm UTC](https://discourse.processing.org/t/if-condition-breaks-for-loop-in-recursive-function/16717 "2019-12-29T21:49:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![musiciantriescoding](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/musiciantriescoding/32/7662_2.png) [@musiciantriescoding](https://discourse.processing.org/u/musiciantriescoding)\
**Post date:** [December 29, 2019, 9:49pm UTC](https://discourse.processing.org/t/if-condition-breaks-for-loop-in-recursive-function/16717/1 "2019-12-29T21:49:43Z")

</div>

Hi everyone! I have come across what I think may be a bug. I am just testing a recursive function which:

1. Draws 6 circles around a circle
2. for each circle, draw 6 circles around it.

This is the following code I wrote. (Counter was for debugging).

```auto
var r;
var counter;
function setup() {
  createCanvas(windowWidth,windowHeight);
  r = height/10;
  counter = 1;
}

function draw() {
  background(255);
  push();
  translate(width/2,height/2);
  noFill();
  rotate(millis()/1000);
  circle(0,0,r);
  circleAround(0,0,r,2);
  pop();
  
  
}

function circleAround(x,y,r,levels){
	let numSides = 6;

	
	for(i=0;i<360;i+=360/6){
		counter++;
		//console.log(counter);
		let circleX = r * cos(radians(i));
		let circleY = r * sin(radians(i));
		push()
		noFill();
		translate(circleX,circleY);
		
		

		if(levels>0){
			
			circleAround(0,0,r,levels-1);
			
		}
		circle(0,0,r);
		pop();
	}
}

```

This produces this output.  
 ![2019-12-29 22.45.22 3c020cbea347](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/54e950da58216ebaf394b3d60b384747177ae432.png)

I found this strange and rewrote the code in Processing. In Processing, the code produced the correct output. (I can’t post a second screenshot since I am a new user)

Is there a mistake in my Javascript? Thank you!

---

<div class="post-metadata">

**Author:** ![musiciantriescoding](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/musiciantriescoding/32/7662_2.png) [@musiciantriescoding](https://discourse.processing.org/u/musiciantriescoding)\
**Post date:** [December 29, 2019, 9:50pm UTC](https://discourse.processing.org/t/if-condition-breaks-for-loop-in-recursive-function/16717/2 "2019-12-29T21:50:18Z")

</div>

![screenshot](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2f5a78c2093326da33c572e424490ce1e4c9c30c.png)

This is the output from Processing!

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [December 29, 2019, 10:27pm UTC](https://discourse.processing.org/t/if-condition-breaks-for-loop-in-recursive-function/16717/3 "2019-12-29T22:27:21Z")

</div>

Inside the function `circleAround` you seem to have forgotten to add `var` to `i` inside the for loop. This caused the issue.

In addition, try replacing `noFill()` with `fill(255,0,0,40)`– were you aware that several ellipses are drawn on top of each other?

---

<div class="post-metadata">

**Author:** ![musiciantriescoding](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/musiciantriescoding/32/7662_2.png) [@musiciantriescoding](https://discourse.processing.org/u/musiciantriescoding)\
**Post date:** [December 30, 2019, 11:08am UTC](https://discourse.processing.org/t/if-condition-breaks-for-loop-in-recursive-function/16717/4 "2019-12-30T11:08:08Z")

</div>

Thanks for pointing that out! I seem to have taken the dynamic typing thing in JS for granted.

Yes, I know that they are overlapping. I finished the sketch and this is the end result:

> **[SAM曹力恒](https://www.instagram.com/p/B6sNL7BgSy7/)**

Thanks again 😁
