Your First Swift 4 & iOS 12 App Online Course – Day 7

Day 7

 

Adding Polish

 

  • Instead of “Hello World!” we will print different messages in the alert box based on how close we get to the random number.
  • If the difference is 0, we will print “Perfect!”. Else if the difference is less than 5, we will print “You almost had it!”. Else if the difference is less than 10, we will print “Pretty good!”. Else we will print “Not even close”.
  • Also, we will award an extra hundred points if the difference is 0 and an extra 50 points if the difference is 1.

The showAlert method looks like this –

 

let difference = abs(currentValue - targetValue)
        var points = 100 - difference
        let title: String
        if difference == 0{
            title = "Perfect!"
            points += 100
        }else if difference < 5 {
            title = "You almost had it!"
            if difference == 1{
                points += 50
            }
        }else if difference < 10 {
            title = "Pretty good..!"
        }else {
            title = "Not even close..."
        }
        
        score += points
        
        let message: String = "You scored: \(points)"
        
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

 

Perfect Score of 200
Perfect Score of 200