Ray Wenderlich Course Part 7 (up to lesson 62)

First of all, I DID IT! I managed to use if statements to program in a maximum with five rounds and the total score being shown. Wasn't easy but I did it!

func nextRound() {
            
            score += points
            roundNumber += 1
        }

        
        if difference == 0 && roundNumber < 5 {
            
            title = "Perfect!"
            points += 100
            nextRound()
            
        } else if difference < 5 && roundNumber < 5 {
            title = "Very close - great try!"
            points += 50
            nextRound()
            
        } else if difference < 10 && roundNumber < 5 {
            title = "Close - good try!"
            nextRound()
            
        } else if difference < 20 && roundNumber < 5 {
            title = "A little bit off!"
            nextRound()
            
        } else if difference < 50 && roundNumber < 5 {
            title = "Eeek...you need to be more accurate!"
            nextRound()
            
        } else if difference < 70 && roundNumber < 5 {
            title = "Oh dear! You have a lot of work to do!"
            nextRound()
            
        } else  {
           message = "You hit \(currentValue). That's \(points) points! \n End of the game! Your total was \(score) out of 500 points!"
            title = "Game over!"
            startOverAgain()
            

        }
So I created a function so that I didn't have to type out the score and round changing; I had to alter the code so that these were put in for each if statement as by having them in the general body of the code meant that it would NEVER get reset for the score and roundNumber. Anyway, I an sure that there are better ways of doing this but this works!

The only issue is that the 'bonus' points aren't adding on. Not sure why but they're not essential so am leaving those for now! I suspect that I could bundle this together into a class so that the code is neater and more succinct. But the fact that I could do this is a real fillip. 

Moving back to the course, the upcoming lessons are about general Swift theory - so as I mentioned at the end of the last entry, a lot of this may repeat. Here we go!

Introduction

Ray has included a 'Swift Cheat Sheet' - a bit like Angela's one! 

He's suggested that if you know some of the theory anyway then watch at double the speed. I'm going to go for 1.5 speed!

Any bits that I wasn't quite so sure about before then of course I will slow down a little. 

There is a reference of the Swift core textbook - I may be using that. We will see!

Swift Playgrounds

As from before, a good way of testing out an algorithim/code etc. 

*An issue I've had before - on the loading bit it says 'launching simulator' and nothing is actually coming up in the sidebar! I've tried opening and closing it but nothing is happening!

OK now it is working!

Comments

Using // for that line.

Using /* for multiple lines and then end with */

You can nest comments if you need to.

The whole purpose is to explain what the code means to yourself or another programmer. I don't admittedly do enough of these. 

Tuples

let coordinates = (4, 5)

I automatically did this without the (Int, Int) - type inference!  The .0 is the value of the first Int, and .1 for the second value. 

let coordinateX = coordinates.0

let coordinateY = coordinates.1

let coordinatesNamed = (x: 3, y: 4)

The last one means we are naming each value, which is much more logical. 

Challenge - Tuples

Excellent! Love these! 

Declare a constant tuple that contains three Int values followed by a Double. Use this to represent a date (month, day, year) followed by an average temperature for that date.

My answer:

let dateAndTemp = (month: 6, day: 27, year: 1988, temperature: 27.5)

In one line, read the day and average temperature values into two constants. You’ll need to employ the underscore to ignore the month and year.

let (_, day, _, averageTemperature) = dateAndTemp

*Got a little stuck on this - checked the previous video. The idea is I can now access 'day' as it's own value!

Up until now, you’ve only seen constant tuples. But you can create variable tuples, too. Change the tuple you created in the exercises above to a variable by using var instead of let. Now change the average temperature to a new value.

*So I have changed the dateAndTemp to a var. Then done this below:

dateAndTemp.averageTemperature = 33.4

Booleans

These can be either true or false etc. etc. 

*I'm ok with all of this. 

One thing - you can use the greater than/less than to compare strings - it will do so alphabetically!

Ternary operator - question mark!

let a = 76
let b = 45

let min = a < b ? a : b
a

This is clever! It's a great way of comparing two values rather than writing out complex if statements.

Challenge - Booleans

Woohoo!

et myAge = 33

 let isTeenager = myAge < 20 && myAge > 12
/*:
 Create another constant called `theirAge` and set it to the age of the author of these challenges (tutorial team member Matt Galloway), which is 30. Then, create a constant called `bothTeenagers` that uses Boolean logic to determine if both you and him are teenagers.
 */

// TODO: Write solution here

 let theirAge = 30

 let bothTeenagers: Bool = isTeenager && (theirAge < 20 && myAge > 12)

/*:
 Create a constant called student and set it to your name as a string. Create a constant called author and set it to Matt Galloway. Create a constant called `authorIsStudent` that uses string equality to determine if student and author are equal.
 */

// TODO: Write solution here

 let student = "Josh"

 let author = "Matt Galloway"

 let authorIsStudent = student == author

/*:
 Create a constant called `studentBeforeAuthor` which uses string comparison to determine if student comes before author.
 */

// TODO: Write solution here

let studentBeforeAuthor = student > author
/*:
 ### IF STATEMENTS AND BOOLEANS
 Create a constant called `myAge` and initialize it with your age. Write an if statement to print out Teenager if your age is between 13 and 19, and Not a teenager if your age is not between 13 and 19.
 */

// TODO: Write solution here

 let myAgeNew = 33

 if myAgeNew > 12 && myAgeNew < 20 {
    
    print("Teenager")
 } else {
    print("Not a teenager")
 }
/*:
 Create a constant called `answer` and use a ternary condition to set it equal to the result you print out for the same cases in the above exercise. Then print out answer.
 */

OK, so this last one I'm stumped with. I'm also not sure if all of these have worked as the playground still claims to be 'running'...

All good apart from the ternary one...

 let answer = myAgeNew > 12 && myAgeNew < 20 ? "Teenager" : "Not a teenager"


OK so I was close! I get the idea now that you actually want to assign values to the true and false conditions (separated with the : ). 

Scope

Local variables can only be used within the curly braces of that. OK. 

var hoursWorked = 45

var price = 0

if hoursWorked > 40 {
    
    let hoursOver40 = hoursWorked - 40
    
    price += hoursOver40 * 50
    
    hoursWorked -= hoursOver40
}

price += hoursWorked * 25

print(price)

So in this example, I can only use the hoursOver40 constant within the curly braces - not outside of them. 

But inside the curly braces, I can use any constant/variable declared outside of them.

All of that makes perfect sense. 

And that's it! I't's been useful doing the challenges and I have learned several new things:
  • The use of the ternary operator
  • Use of bools with strings
  • A bit more about tuples and changing their values
  • Use of local variables and scope


Next time it will be about loops - something I do need more practice with and understanding about - especially in context. Good stuff! 






Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)