Welcome back to another edition of {Kids: Code}, the free weekly coding newsletter! For the past several weeks we’ve been exploring all the different ways you can build a website using HTML, CSS, and JavaScript, so we decided that we’re going to tackle something a bit different this time: coding in Swift for iPhones, iPads and Macs! Plus, we’ve got a Microsoft virtual summer camp to share and some suggestions for JavaScript projects to tackle.
Want to reach out to us? You can contact us here or check us out on Twitter!
This week, we're taking a break from our HTML, CSS, and JS tutorials to spotlight another coding language: Swift!
What's Swift?
Swift is one of the main coding languages used if you want to build apps for iPhones, iPads, and Macs. Writing it can feel like JavaScript at points, so if you like that, you might enjoy Swift. Apple recently announced that Swift Playgrounds can be used to publish apps as well! If you have an iPad or Mac, you can download the Swift Playgrounds App to try it out.
Using Android?
We'll cover an equivalent app for Android next week, so stay tuned!
Coding in Your Playground
When you open the Playgrounds app, you can choose to start a blank playground, or open an example playground to mess around with. Both are fantastic options for learning! For our quick tutorial below, we'll be using a blank playground to show how to use the app a bit more. Select the blank playground to open it for coding.
What's in a Name?
For our small example, let's take a list of names and put them in alphabetical order.
var names = ["Bert", "Andrew", "Zack"]
print("Names: \(names)")
names.sort()
print("Alphabetized: \(names)")
Going line by line:
Our first line defines our variable, the list of names. You could almost copy and paste this into a JavaScript project!
On our second line, we print our variable. The one trick to explain here: variables need to be surrounded in
\(
and)
to display properly.Third line: we sort the list, with a built-in
.sort()
method.Fourth line: we print the list, which is now sorted.
Click the Run My Code
button to run your code and see the output, which should look something like the screenshot below:
Learning in Swift Playgrounds
One cool thing about Swift Playgrounds is that you can break your code to learn. Sounds crazy, right? Let's try it and see.
Let's change our first line from using a var
to a let
, like so:
From this:
var names = ["Bert", "Andrew", "Zack"]
To this:
let names = ["Bert", "Andrew", "Zack"]
You should start to see some red dots appear in the left side of your editor!
This is Swift Playgrounds telling you what's wrong with the code. We can use this to learn what's going on by clicking or tapping on the red dots:
Here, Swift Playgrounds is telling us that a let
variable can't be changed after it's set. In programming, this is often referred to as mutation - and is why Swift Playgrounds says that a let
is immutable, or unable to be mutated. The fix? Change our code back to using var
! Now we know what not to do though.
Swift Playgrounds will auto-suggest different pieces of code for you to try, and the example projects are an amazing resource for bumping your skills. Try it out and see what you can build!
Stuck at home all summer? Or maybe you aren’t, but still want to try something new? Fortunately for everyone, Microsoft has announced a “virtual summer camp” that allows for participants to practice their love of archaeology, travel, and - you guessed it! - computer programming.
Two of the programs shown allow for students to use block-based coding programs like MakeCode to help build your own games from home! You can check out more about the program with this article and see the full catalogue of courses they’ll be offering here.
JavaScript Projects for Beginners
By now, you’ve probably realized how important a language JavaScript is to coding things on the Internet, right? We’ve shown you the basics of it and even used it to build a website! We want to take the time to highlight a few more projects you can practice with.
JavaScript can be used for a variety of different things - like building a quiz or a digital clock. You can also have JavaScript generate random colors for you. You can even use it to create a To-Do list, a photo gallery, and a flip card memory game. These are just some of the more simple projects you can use to practice your coding game, but they won’t be the last. We’ll be sure to keep bringing you more great ideas for other languages as well like HTML and Python, so stay tuned!
This Week’s Vocabulary Word
Sorting
A common approach in coding is to sort a list of items. This can be through built-in sorting algorithms (for example, alphabetical) or your own custom algorithm.