Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift 2.0 Basics Swift Operators Working With Operators: Part 2

Tejas Manohar
Tejas Manohar
1,456 Points

Why should I use the "NOT operator" (`!`) here?

I'm not quite sure why I'd use the "NOT operator" in this challenge. The way I've solved it seems to produce correct results, but the automated grader complains, "Bummer! Make sure you increment the value of initialScore just once" and the hint says "Use the NOT operator" so thought I'd post here to see what's up.

4 Answers

Helgi Helgason
Helgi Helgason
7,599 Points

the totalScore is 9 not 10 so the player is not a winner! In the game if you have anything other than 10 you lose so you need to check if totalScore is anything other than 10 using != meaning isWinner is false.

Tejas Manohar
Tejas Manohar
1,456 Points

I selected the option to attach my solution to this post, but I don't see it here so I've included it below.

var initialScore = 8
let totalScore = ++initialScore
let isWinner = totalScore == 10
Tejas Manohar
Tejas Manohar
1,456 Points

the totalScore is 9 not 10 so the player is not a winner! In the game if you have anything other than 10 you lose so you need to check if totalScore is anything other than 10 using != meaning isWinner is false.

let isWinner = totalScore == 10

Hm, why would I use != for this? In the example code I've included above, isWinner will be false because totalScore == 10 evaluates to false. I would use != if the condition was notWinner. If you really want to use !=, you can do

let isWinner = !(totalScore != 10)

or

use an if (totalScore != 10)/else block

... but that just seems overly complex compared to what I have above :P - Am I misreading the question, or is the solution I've provided not logically correct? It provides the correct result so not really sure what I'm missing. I suspect this is an issue with the challenge's grader.

cameron swenson
cameron swenson
9,951 Points

The code challenge states: "At the end of the round, if your score is 10, you lose! If it's anything but 10, you win."

isWinner = totalScore == 10

result would be false because 9 does not equal 10, but the player should be a winner.

isWinner = totalScore != 10 

result would be true because 9 does equal "not 10", so they are a winner, which is the result we are expecting.

Tejas Manohar
Tejas Manohar
1,456 Points

Ah misread too many times-- my bad. Thought it was if it's 10, you win :P