create account

Turtle Programming: While Loop, Do/Else Loop and Unit Tests Added by justyy

View this thread on: hive.blogpeakd.comecency.com
· @justyy · (edited)
$70.88
Turtle Programming: While Loop, Do/Else Loop and Unit Tests Added
## Introduction to Logo Turtle
[LogoTurtle](https://helloacm.com/turtle-programming-while-loop-do-else-loop-and-unit-tests-added/) is currently the FIRST and only one Chrome Extension for Turtle Graphics. I have also written [a PHP version of Logo Interpreter](https://steakovercooked.com/Software.Logo) in 2006 but that runs only on the server.

![](https://steemitimages.com/DQmXSwCpG14nWsRsfhbjPiCSmdGcLHLCriXHf5pFxJk1DVo/image.png)

## Previous Contributions
- v0.0.10: [Turtle Programming: Fractal Stars, Random, Console, Eraser, SetPC, SetXY, Examples, Wait, Bug Fixes and So much more!](https://helloacm.com/turtle-programming-fractal-stars-random-console-eraser-setpc-setxy-examples-wait-bug-fixes-and-so-much-more/)
- v0.0.9: [Turtle Programming v0.0.9: Add SetX, SetY, Square and Rect!](https://helloacm.com/turtle-programming-v0-0-9-add-setx-sety-square-and-rect/)
- v0.0.8: [Turtle Programming v0.0.8: /* */ comments, dotxy, and javascript!](https://helloacm.com/turtle-programming-v0-0-8-comments-dotxy-and-javascript/)
- v0.0.7: [Turtle Programming v0.0.7:  Functions with Parameters + Recursion!](https://helloacm.com/turtle-programming-v0-0-7-functions-with-parameters-recursion/)
- v0.0.6: [Turtle Programming v0.0.6: Adding Circle, MoveTo, Turn and Screen!](https://helloacm.com/turtle-programming-v0-0-6-adding-circle-moveto-turn-and-screen/)
- v0.0.5: [Turtle Programming v0.0.5: Adding IF/ELSE and STOP!](https://helloacm.com/turtle-programming-v0-0-5-adding-if-else-and-stop/)
- v0.0.4: [LogoTurtle: Make Variables and Comments](https://helloacm.com/logoturtle-make-variables-and-comments/)
- v0.0.3: [Turtle Graphics Programming Update: Adding text, jump, dot, fontsize, download as png](https://helloacm.com/turtle-graphics-programming-update-adding-text-jump-dot-fontsize-download-as-png/)
- v0.0.2: [LogoTurtle v0.0.2: ShowTurtle, HideTurtle, Color, Width and Help](https://helloacm.com/logoturtle-v0-0-2-showturtle-hideturtle-color-width-and-help/).
- Teach Your Kids Programming - The First Logo Interpreter (Turtle Graphics) in Chrome Extension!
 [v0.0.1](https://helloacm.com/teach-your-kids-programming-the-first-logo-interpreter-turtle-graphics-in-chrome-extension/)

## v0.0.11 New Features
[**This Commit**](https://github.com/DoctorLai/LogoTurtle/commit/cf2f99af086d25927f5d280f59e314c7b6c913dc) has the following changes:

1. Add While Loop
2. Add Do/Else Loop
3. SetXY - Y Coordinate reversed to match math coordinate.
4. Understore is allowed in variable names.
5. Add global variables: *turtlex*, *turtley* and *turtleangle*
6. Add Unit Tests `npm test`

## Screenshots
In LOGO programming language, there isn't a While Loop. The boolean expression is evaluated per loop iteration. For example:

```
make "x 0
make "sum 0
while :x<=100 [
  make "sum :sum+:x
  make "x :x+1
]
text [sum is :sum]
```

The Do/Else is a syntax sugar as `if (condition) { while (condition) { /* loop */} } else { / * else */}`
```
cs ht make "x 10
do :x<4 [
  fd 100 rt 90 
  make "x :x+1
] else [
  repeat 5 [fd 100 rt 144]
]
```

The above draws a square if `x=0` and a star if `x` is set to above 4.

```
to spiral :size
  if (:size>30) [stop]  ; an exit condition
  fd :size rt 15        ; many lines of action
  spiral :size*1.02    ; the tailend recursive call
end
```

can be re-written in non-recursive DO loop (the else is optional). 

```
to spiral :size
  do :size<=30 [
     fd :size rt 15  
     make "size :size*1.02
  ] else [
     console [:size is larger than 30]
  ]
end
```

![image.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520998145/ilfyzujaw2x6ittqiojc.png)

## Implement the LOGO/DO loop in Javascript
```
case "do":
	find_left = getNextWord(s, y.next, U);
	if (find_left.word != '[') {
		this.pushErr(word, LOGO_ERR_MISSING_LEFT, find_left.word);
		return false;
	}
	repeat_left = find_left.next;
	find_right = repeat_left + 1;
	nested = 1;
	// need to match [ and ]
	while (find_right < U) {
		if (s[find_right] == '[') {
			nested ++;
		}												
			if (s[find_right] == ']') {
				nested --;
				if (nested == 0) {
					break;
				}
			}
			find_right ++;
	}
	if (find_right >= U) {
		this.pushWarning(word, LOGO_ERR_MISSING_RIGHT);						
	}
	let do_exp = word_next;													
	word_next = this.evalVars(do_exp);			
	ifelse = iftrue(word_next);
	if (word_next === '') {
		this.pushErr(word, LOGO_ERR_MISSING_EXP, word_next);
		return false;
	}												
	while (iftrue(word_next)) {
		// do body
		if (!this.run(s, repeat_left, find_right, depth + 1)) {		
			return false;
		}
		word_next = this.evalVars(do_exp);
	} 	
	find_else = getNextWord(s, find_right + 1, U);
	if (find_else.word.toLowerCase() == 'else') {
		let else_block = getNextBody(s, find_else.next, U);
		if (else_block.ch != '[') {
			this.pushErr(word, LOGO_ERR_MISSING_LEFT, else_block.ch);
			return false;
		}
		if (!ifelse) {
			// else body
			if (!this.run(s, else_block.left, else_block.right, depth + 1)) {
				return false;
			}
		}
		i = else_block.right + 1;
	} else {
		// no else block
		i = find_right + 1; 
	}
	break;	
```

## Roadmap of Chrome Extension: Logo Turtle
I believe LogoTurtle is more or less in beta now. Therefore, bug Fixes and any suggestions, please shout @justyy

## Technology Stack
If an App can be written in [Javascript](https://helloacm.com/steemit-javascript-function-to-get-original-post-from-comments-permlink/), eventually it will be written in Javascript.

# Chrome Webstore
Install the [Turtle Programming for Kids](https://chrome.google.com/webstore/detail/logo-turtle/dcoeaobaokbccdcnadncifmconllpihp) Now!

# Contribution Welcome
Github: https://github.com/DoctorLai/LogoTurtle
1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request.


<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@justyy/turtle-programming-while-loop-do-else-loop-and-unit-tests-added">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 46 others
πŸ‘Ž  
properties (23)
authorjustyy
permlinkturtle-programming-while-loop-do-else-loop-and-unit-tests-added
categoryutopian-io
json_metadata"{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":121791502,"name":"LogoTurtle","full_name":"DoctorLai/LogoTurtle","html_url":"https://github.com/DoctorLai/LogoTurtle","fork":false,"owner":{"login":"DoctorLai"}},"pullRequests":[],"platform":"github","type":"development","tags":["utopian-io","steemstem","programming","cn-programming","steemapps"],"users":["justyy"],"links":["https://helloacm.com/turtle-programming-while-loop-do-else-loop-and-unit-tests-added/","https://steakovercooked.com/Software.Logo","https://helloacm.com/turtle-programming-fractal-stars-random-console-eraser-setpc-setxy-examples-wait-bug-fixes-and-so-much-more/","https://helloacm.com/turtle-programming-v0-0-9-add-setx-sety-square-and-rect/","https://helloacm.com/turtle-programming-v0-0-8-comments-dotxy-and-javascript/","https://helloacm.com/turtle-programming-v0-0-7-functions-with-parameters-recursion/","https://helloacm.com/turtle-programming-v0-0-6-adding-circle-moveto-turn-and-screen/","https://helloacm.com/turtle-programming-v0-0-5-adding-if-else-and-stop/","https://helloacm.com/logoturtle-make-variables-and-comments/","https://helloacm.com/turtle-graphics-programming-update-adding-text-jump-dot-fontsize-download-as-png/","https://helloacm.com/logoturtle-v0-0-2-showturtle-hideturtle-color-width-and-help/","https://helloacm.com/teach-your-kids-programming-the-first-logo-interpreter-turtle-graphics-in-chrome-extension/","https://github.com/DoctorLai/LogoTurtle/commit/cf2f99af086d25927f5d280f59e314c7b6c913dc","https://helloacm.com/steemit-javascript-function-to-get-original-post-from-comments-permlink/","https://chrome.google.com/webstore/detail/logo-turtle/dcoeaobaokbccdcnadncifmconllpihp","https://github.com/DoctorLai/LogoTurtle","https://utopian.io/utopian-io/@justyy/turtle-programming-while-loop-do-else-loop-and-unit-tests-added"],"image":["https://steemitimages.com/DQmXSwCpG14nWsRsfhbjPiCSmdGcLHLCriXHf5pFxJk1DVo/image.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520998145/ilfyzujaw2x6ittqiojc.png"],"moderator":{"account":"decebal2dac","time":"2018-03-15T00:07:18.863Z","reviewed":true,"pending":false,"flagged":false},"questions":[{"question":"Is the project description formal?","answers":[{"value":"Yes it’s straight to the point","selected":true,"score":10},{"value":"Need more description ","selected":false,"score":5},{"value":"Not too descriptive","selected":false,"score":0}],"selected":0},{"question":"Is the language / grammar correct?","answers":[{"value":"Yes","selected":false,"score":20},{"value":"A few mistakes","selected":true,"score":10},{"value":"It's pretty bad","selected":false,"score":0}],"selected":1},{"question":"Was the template followed?","answers":[{"value":"Yes","selected":true,"score":10},{"value":"Partially","selected":false,"score":5},{"value":"No","selected":false,"score":0}],"selected":0},{"question":"How do you rate the amount of work?","answers":[{"value":"Very High","selected":true,"score":20},{"value":"High","selected":false,"score":16},{"value":"Medium","selected":false,"score":12},{"value":"Low","selected":false,"score":7},{"value":"Very Low","selected":false,"score":3}],"selected":0},{"question":"How do you rate the impact on the Project?","answers":[{"value":"Very High","selected":false,"score":20},{"value":"High","selected":true,"score":16},{"value":"Medium","selected":false,"score":12},{"value":"Low","selected":false,"score":7},{"value":"Very Low","selected":false,"score":3}],"selected":1}],"score":100}"
created2018-03-14 03:31:48
last_update2018-03-15 00:07:18
depth0
children4
last_payout2018-03-21 03:31:48
cashout_time1969-12-31 23:59:59
total_payout_value51.371 HBD
curator_payout_value19.510 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,079
author_reputation280,616,224,641,976
root_title"Turtle Programming: While Loop, Do/Else Loop and Unit Tests Added"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id44,276,523
net_rshares28,791,242,766,427
author_curate_reward""
vote details (111)
@decebal2dac ·
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authordecebal2dac
permlinkre-justyy-turtle-programming-while-loop-do-else-loop-and-unit-tests-added-20180315t000723958z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-03-15 00:07:24
last_update2018-03-15 00:07:24
depth1
children0
last_payout2018-03-22 00:07:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation13,975,053,566,819
root_title"Turtle Programming: While Loop, Do/Else Loop and Unit Tests Added"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id44,478,116
net_rshares0
@maanabdullah ·
Great information here love your post
properties (22)
authormaanabdullah
permlinkre-justyy-turtle-programming-while-loop-do-else-loop-and-unit-tests-added-20180314t033641069z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-03-14 03:36:57
last_update2018-03-14 03:36:57
depth1
children0
last_payout2018-03-21 03:36:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length37
author_reputation4,394,544,845,307
root_title"Turtle Programming: While Loop, Do/Else Loop and Unit Tests Added"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id44,277,259
net_rshares0
@utopian-io ·
### Hey @justyy I am @utopian-io. I have just upvoted you!
#### Achievements
- WOW WOW WOW People loved what you did here. GREAT JOB!
- Seems like you contribute quite often. AMAZING!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
authorutopian-io
permlinkre-justyy-turtle-programming-while-loop-do-else-loop-and-unit-tests-added-20180315t201706333z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-03-15 20:17:48
last_update2018-03-15 20:17:48
depth1
children0
last_payout2018-03-22 20:17:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,061
author_reputation152,955,367,999,756
root_title"Turtle Programming: While Loop, Do/Else Loop and Unit Tests Added"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id44,658,307
net_rshares0
@utopian-io ·
### Hey @justyy I am @utopian-io. I have just upvoted you!
#### Achievements
- WOW WOW WOW People loved what you did here. GREAT JOB!
- Seems like you contribute quite often. AMAZING!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
authorutopian-io
permlinkre-justyy-turtle-programming-while-loop-do-else-loop-and-unit-tests-added-20180315t201929145z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-03-15 20:20:00
last_update2018-03-15 20:20:00
depth1
children0
last_payout2018-03-22 20:20:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,061
author_reputation152,955,367,999,756
root_title"Turtle Programming: While Loop, Do/Else Loop and Unit Tests Added"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id44,658,580
net_rshares0