If / Else won't run

31 views Asked by At

I am working with the IF/Else training in Udemy and cannot see where my error/issue is. When I run the code, nothing happens and it doesn't activate an error(in XCode)

When I apply it to the (online)playground, it's identifying almost everything as an error. I'm a beginner. I'm follwong the lessons but if she doesn't supply the solution, I can't see where I went wrong. Help.

import UIKit

class ViewController: UIViewController {
    
    let softTime = 5
    let mediumTime = 7
    let hardTime = 12
    
    @IBAction func hardnessSelected(_ sender: UIButton) {
    
        let hardness = sender.currentTitle
    }
    func hardness(temp: Int){
        let hardness = Int.random(in: 0 ... 12)

        if hardness >= 10 {
            print("Hard Boiled")
        };if hardness <= 9 {
            print("Medium Boiled")
        } else {
            print("Soft Boiled")
        }
    }
} 
1

There are 1 answers

0
ErykK On

You have an unnecessary semi-colon (';') on your second if, also you should use else if when you have another condition you want to check for. Try this code:

import UIKit

class ViewController: UIViewController {
    
    let softTime = 5
    let mediumTime = 7
    let hardTime = 12
    
    @IBAction func hardnessSelected(_ sender: UIButton) {
    
        let hardness = sender.currentTitle
    }
    func hardness(temp: Int){
        let hardness = Int.random(in: 0 ... 12)

        if hardness >= 10 {
            print("Hard Boiled")
        } else if hardness <= 9 {
            print("Medium Boiled")
        } else {
            print("Soft Boiled")
        }
    }
} 

You can read more about javascript if statements here: https://www.w3schools.com/js/js_if_else.asp