HW#18 Practice of Code Writing

Getting more familiar with Variables via Playgrounds App

A while ago, Peter has mentioned Playgrounds and encouraged us to practice coding via games. Today, I’m going to show some interesting parts in Learn to Code 2 with this cute little alien-like creature collecting gems or toggling switches.

For someone like me who doesn’t really play video games a lot, this is relatively fun and refreshing to practice writing code.

First, go to Playgrounds App and click Learn to Code 2. Here mine is written in Mandarin before I switched the language to English.

Second, on the left sidebar, you will see the contents of Chapters. The first one is Variables. Read the brief Introduction to get some basic ideas of Variables.

Third, start with the first challenge Keeping Track! This one is quite easy if you rememer what we have learnt about var in class. I used moveForward() to make the creature move one step and since we have declared var gemCounter = 0, then we could write gemCounter = 1 at the end.

Fourth, the second challenge Bump Up the Value. The challenge is to assign a new value to gemCounter to each additional gem collected. Again, we set var gemCounter = 0 to begin with.

Fifth, the third challenge is Incrementing the Value. The goal here is to increment your variable to track the number of gems collected. Here I learnt how to use if, while and !(means not). I actually had to search some info on the Internet to get better understanding. Although it took me some time, it was worth it.

Sixth, the fourth challenge is Seeking Seven Gems. The goal is to find exactly seven gems. Here we need to use a comparison operator :

while gemCounter < 7 { }

Seventh, the fifth challenge is Three Gems, Four Switches. The goal is to collect exactly three gems and toggle open four switches. To begin with we need to have two separate variables; one is for the number of gems, the other is for the number of switches. We also need to use more comparison operators here. Luckily in the introduction, it is clearly explain what we have to do and the meaning of “less than operator (a <b) returns true if a is less than b.” Here it reminds us that equal to operator is written like (a ==b) & not equal to is (a !=b).

Eighth, the sixth challenge is Checking for Equal Values. The goal is to collect as many gems as there are switches. Here we’ll use a constant called switchCounter (declare with let) . In this challenge, we again will use some comparison operators.

Ninth, the seventh challenge is Round up the Switches. The goal is to toggle the same number of switches as gems collected. Here we use compound assignment operator to add a value to a variable and assign the new value at the same time. One thing we can do is shorten the assignment:

gemCounter = gemCounter +1 — — >gemCounter += 1

Last, the eighth challenge is Collect the Total. The goal is to collect a randomly determined number of gems, represented by totalGems.

Here we use a constant totalGems:

let totalGems = randomNumberOfGems

This challenge took me some time as I tried to find out how to make the creature turn around when there is no way in front of it. I then figured out writing turnLeft() twice. It is a good review of what we have done previously.

I did enjoy this practice and for those who want to get more familiar with coding, this game-oriented playgrounds is definitely a top pick! Until next time, cheers!

--

--

Jerski
彼得潘的 Swift iOS / Flutter App 開發教室

Studied Linguistics in university and have been intrigued by code writing. Now this journey of Swift just began.