Missing a bracket, etc. (Swift)

307 Views Asked by At

Hi guys Im having trouble figuring out whats wrong. In the following function, when the category is Subtraction and the level Hard it skips past the function and goes to the division part. All my wording i believe is correct but I think something is wrong with my brackets. Spent over an hour trying to find it. Still can't. And btw, all the other categories and levels work, even in subtraction. The only false one is hard subtraction and I can't find what's wrong with my brackets.

Code:

func generate(){


    if category == "Multiplication"{
        if level == "Easy"{
    part1 = Int(arc4random_uniform(UInt32(4)))
    part2 = Int(arc4random_uniform(UInt32(4)))
    questionsLbl.text = "\(part1) x \(part2)?"
    answer = part1 * part2
    createExtraAnswers()
        }
        if level == "Medium"{
            part1 = Int(arc4random_uniform(UInt32(10)))
            part2 = Int(arc4random_uniform(UInt32(10)))
            questionsLbl.text = "\(part1) x \(part2)?"
            answer = part1 * part2
            createExtraAnswers()
        }
        if level == "Hard"{
            part1 = Int(arc4random_uniform(UInt32(14)))
            part2 = Int(arc4random_uniform(UInt32(14)))
            questionsLbl.text = "\(part1) x \(part2)?"
            answer = part1 * part2
            createExtraAnswers()
        }
    }else{
        if category == "Addition"{
            if level == "Easy"{
            part1 = Int(arc4random_uniform(UInt32(6)))
            part2 = Int(arc4random_uniform(UInt32(6)))
            questionsLbl.text = "\(part1) + \(part2)?"
            answer = part1 + part2
            createExtraAnswers()
            }
            if level == "Medium"{
                part1 = Int(arc4random_uniform(UInt32(20)))
                part2 = Int(arc4random_uniform(UInt32(20)))
                questionsLbl.text = "\(part1) + \(part2)?"
                answer = part1 + part2
                createExtraAnswers()
            }
            if level == "Hard"{
                part1 = Int(arc4random_uniform(UInt32(100)))
                part2 = Int(arc4random_uniform(UInt32(100)))
                questionsLbl.text = "\(part1) + \(part2)?"
                answer = part1 + part2
                createExtraAnswers()
            }
        }else{
            if category == "Subtraction"{
                if level == "Easy"{
                part1 = Int(arc4random_uniform(UInt32(10)))
                part2 = Int(arc4random_uniform(UInt32(10)))
                questionsLbl.text = "\(part1) - \(part2)?"
                answer = part1 - part2
                    if answer <= -1{
                        generate()
                    }
                createExtraAnswers()
                }
                if level == "Medium"{
                    part1 = Int(arc4random_uniform(UInt32(25)))
                    part2 = Int(arc4random_uniform(UInt32(25)))
                    questionsLbl.text = "\(part1) - \(part2)?"
                    answer = part1 - part2
                    if answer <= -1{
                        generate()
                    }
                    createExtraAnswers()
                }
                if level == "Hard"{
                    part1 = Int(arc4random_uniform(UInt32(100)))
                    part2 = Int(arc4random_uniform(UInt32(100)))
                    questionsLbl.text = "\(part1) - \(part2)?"
                    answer = part1 - part2

                    createExtraAnswers()

                }

            }else{
                if category == "Division"{
                    if level == "Easy"{
                    part1 = Int(arc4random_uniform(UInt32(37)))
                    part2 = Int(arc4random_uniform(UInt32(37)))
                    questionsLbl.text = "\(part1) / \(part2)?"
                    answerDub = Double(Double(part1) / Double(part2))
                  if (answerDub % 1 == 0) {
                        print("whole number confirmed")
                    print(answerDub)
                        answer = Int(answerDub)
                    print(answer)
                        createExtraAnswers()
                    } else {
                        generate()
                    }
                }
              }
                if level == "Medium"{
                    part1 = Int(arc4random_uniform(UInt32(80)))
                    part2 = Int(arc4random_uniform(UInt32(80)))
                    questionsLbl.text = "\(part1) / \(part2)?"
                    answerDub = Double(Double(part1) / Double(part2))
                    if (answerDub % 1 == 0) {
                        print("whole number confirmed")
                        print(answerDub)
                        answer = Int(answerDub)
                        print(answer)
                        createExtraAnswers()
                    } else {
                        generate()
                    }
                }
            }
            if level == "Hard"{
                part1 = Int(arc4random_uniform(UInt32(140)))
                part2 = Int(arc4random_uniform(UInt32(140)))
                questionsLbl.text = "\(part1) / \(part2)?"
                answerDub = Double(Double(part1) / Double(part2))
                if (answerDub % 1 == 0) {
                    print("whole number confirmed")
                    print(answerDub)
                    answer = Int(answerDub)
                    print(answer)
                    createExtraAnswers()
                } else {
                    generate()
                }
            }


        }
    }
}
3

There are 3 best solutions below

2
Sumner Evans On

The problem is in your if category = "Division" code:

if category == "Division"{
    if level == "Easy"{
        part1 = Int(arc4random_uniform(UInt32(37)))
        part2 = Int(arc4random_uniform(UInt32(37)))
        questionsLbl.text = "\(part1) / \(part2)?"
        answerDub = Double(Double(part1) / Double(part2))
        if (answerDub % 1 == 0) {
            print("whole number confirmed")
            print(answerDub)
            answer = Int(answerDub)
            print(answer)
            createExtraAnswers()
        } else {
            generate()
        }
    }
} // the if category == "Division" statement is ending here.

Remove that bracket (the one I put a comment next to) and move it below the if level == "Hard" statement.

As a side note, your code is very poorly formatted. It is much easier to find errors if you have all of your blocks lined up.

3
0x416e746f6e On

Nested if-else statements are hell. Why don't you use switch statement like this:

switch (category, level) {
case ("Multiplication", "Easy"):
    part1 = Int(arc4random_uniform(UInt32(4)))
    part2 = Int(arc4random_uniform(UInt32(4)))
    questionsLbl.text = "\(part1) x \(part2)?"
    answer = part1 * part2
    createExtraAnswers()
case ("Multiplication", "Medium"):
    part1 = Int(arc4random_uniform(UInt32(10)))
    part2 = Int(arc4random_uniform(UInt32(10)))
    questionsLbl.text = "\(part1) x \(part2)?"
    answer = part1 * part2
    createExtraAnswers()
// ...
}

It's much more readable this way.

0
the_critic On

I think the if-else statements make for ugly unreadable code. Your use-case requires you to have some type of "ugliness" in the code due to the fact that you have to hard-code stuff, so here is a little playground I came up with:

enum Difficulty{
  case Easy
  case Medium
  case Hard
}

protocol ExerciseProtocol{

  var difficulty : Difficulty {get}
  var description : String {get}
  var answer : Int {get}

}

class MathExercise : ExerciseProtocol {

  enum ExerciseType : String {
    case Multiplication = "x"
    case Addition = "+"
    case Subtraction = "-"
    case Division = "/"

  }

  var difficulty : Difficulty
  var type : ExerciseType
  var operand1 : Int
  var operand2 : Int
  var description : String{

    return "\(operand1) \(self.type.rawValue) \(operand2)"

  }

  init(type: ExerciseType, difficulty: Difficulty){

    self.difficulty = difficulty
    self.type = type

    switch difficulty{
    case .Easy:
      switch type{
      case .Addition:
        operand1 = Int(arc4random_uniform(UInt32(6)))
        operand2 = Int(arc4random_uniform(UInt32(6)))
      case .Subtraction:
        operand1 = Int(arc4random_uniform(UInt32(10)))
        operand2 = Int(arc4random_uniform(UInt32(10)))
      case .Multiplication:
        operand1 = Int(arc4random_uniform(UInt32(4)))
        operand2 = Int(arc4random_uniform(UInt32(4)))
      case .Division:
        operand1 = Int(arc4random_uniform(UInt32(37)))
        operand2 = Int(arc4random_uniform(UInt32(37)))

      }
    case .Medium:
      switch type{
      case .Addition:
        operand1 = Int(arc4random_uniform(UInt32(20)))
        operand2 = Int(arc4random_uniform(UInt32(20)))
      case .Subtraction:
        operand1 = Int(arc4random_uniform(UInt32(25)))
        operand2 = Int(arc4random_uniform(UInt32(25)))
      case .Multiplication:
        operand1 = Int(arc4random_uniform(UInt32(10)))
        operand2 = Int(arc4random_uniform(UInt32(10)))
      case .Division:
        operand1 = Int(arc4random_uniform(UInt32(80)))
        operand2 = Int(arc4random_uniform(UInt32(80)))

      }
    case .Hard:
      switch type{
      case .Addition:
        operand1 = Int(arc4random_uniform(UInt32(100)))
        operand2 = Int(arc4random_uniform(UInt32(100)))
      case .Subtraction:
        operand1 = Int(arc4random_uniform(UInt32(100)))
        operand2 = Int(arc4random_uniform(UInt32(100)))
      case .Multiplication:
        operand1 = Int(arc4random_uniform(UInt32(14)))
        operand2 = Int(arc4random_uniform(UInt32(14)))
      case .Division:
        operand1 = Int(arc4random_uniform(UInt32(140)))
        operand2 = Int(arc4random_uniform(UInt32(140)))

      }
  }

  }

  var answer : Int{
    switch self.type{
    case .Addition:
      return operand1 + operand2
    case .Subtraction:
      return operand1 - operand2
    case .Multiplication:
      return operand1 * operand2
    case .Division:
      return operand1 / operand2
    }
  }
}

And use it like this:

let division = MathExercise(type: .Division, difficulty: .Hard)
division.description // prints something like "5 / 5"
division.answer // prints "1"