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 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?

operators.swift
// 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") }