Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Basics Swift Operators Working With Operators: Part 2

Hello, I'm not entirely sure how to solve this task without using an else-if statement. Can someone further instruct me?

I'm having trouble trying to write the code from the given instructions. Do I need to add any other constants or variables, or add any other operators?

// Enter your code below

var initialScore = 8
initialScore += 1 

var totalScore 
let isWinner 

1 Answer

Fletcher Henneman
Fletcher Henneman
3,130 Points

When using an if statement, it merely evaluates if the condition specified is true or false. What you want to do is set the constant isWinner to that evaluation, instead of making if and else-if statements. If your score is 10, you loose. If the score is NOT 10, you win! So you want to make isWinner equal to totalScore != 10. That condition will return true if your score is NOT ten meaning you won because (for example 11) 11 does Not ( != ) 10. If your score is ten, it will return false. Then the variable isWinner will be set to either true/false depending on your totalScore

Here is the actual answer:

var initialScore = 8
initialScore += 1

var totalScore = 5
let isWinner = (totalScore != 10) //The parenthesis surrounding the condition is not necessary

Thank you, I knew I was overthinking this. I put this and it worked too.

var finalScore = 11 var isWinner = true || false if finalScore == 10 { let isWinner = false print("you lose") } else { print("winner") }