Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 1: Core Concepts

04. Challenge: Booleans

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 03. Booleans & Comparison Operators Next episode: 05. Logical Operators

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 04. Challenge: Booleans

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Hey, it's time for your first challenge. You can find the challenge in the Challenge Booleans part of the playground you've been using, or you can download a new one from the resources for this video. If you open it up, you'll see that there are a few mini exercises in there to give you some practice using booleans on your own. So try solving the challenge, questions on your own, and then keep watching to compare your work to mine. Good luck. (bouncy music) Okay, Challenge 1, create a constant named myAge and set its value to your age. Then create a constant named isVotingAge that uses Boolean logic to determine if a value stored in myAge denotes someone of voting age. So in my part of the world, the voting age is 18. So I'll use that here. Okay, first I'll set myAge, which is currently 42, and then I need to check if myAge is greater than or equal to 18, the voting age, and assign that to the new variable. And it's true, Okay, On to Challenge 2. Create a constant named student, and set its value to your name as a string. So next create a constant named author, and set its value to "Matt Galloway", the original author of these exercises. Thank you, Matt. And then create a third constant named authorIsStudent that uses string equality to determine if the values of student and author are equal. Okay, first I'll set up the student and author constants, and then to find out if the author and student have the same name, I'll use the "=" check for equality, and lol, I am not, Matt Galloway. On to challenge 3. Create a constant name studentBeforeAuthor, which uses string comparison to determine if the string value in the constant student comes alphabetically speaking, before the string value in the constant author that you declared above. So the constants student and author were declared above in Challenge 3, so you don't have to declare them here. Now, this is a short one, to compare the strings, I need to use the "<" or ">" operator. So I used "<" because that will tell us if student comes before author, because with strings, "<" really means comes earlier in the dictionary. And that's it. Now you could also have written this using the ">" operator. However, when it comes to "<", and ">" signs, it's best to use the arrangement that makes it most clear what you're trying to do. In this case, the constant is named studentBeforeAuthor, not authorAfterStudent. So makes more sense here to use a previous form, which reads like if student < author, that's it for this challenge. Next step, some more work with operators and booleans.