Select Page

Crazy Level

Swift and Xcode Resources.

Optionals Example Code
Published on: February 21, 2021
Author: Frank Vernon
// 2 ways to print the highest score...
var studentsAndScores = ["Amy": 88, "James": 55, "Helen": 99]

func highestScore(scores: [String: Int]) {
  
  //OPTION 1 BELOW HERE!

    let a = studentsAndScores ["Amy"]!
    let b = studentsAndScores ["James"]!
    let c = studentsAndScores ["Helen"]!

    var temp = 0
    
    if a > temp {
        temp = a
    }
    
    if b > temp {
        temp = b
    }
    
    if c > temp {
        temp = c
    }
    
    print(temp)
 
    //OPTION 1 ABOVE HERE!
    //OPTION 2 BELOW HERE!
    
//    if a > b && a > c {
//    print(a)
//    }
//
//    else if b > c {
//    print(b)
//    }
//    else {
//    print(c)
//    }
  
    //OPTION A ABOVE HERE!
    
}

highestScore(scores: studentsAndScores)