-3

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()
                }
            }


        }
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Andy Lebowitz
  • 1,471
  • 2
  • 16
  • 23
  • 4
    Have you tried to single-step through your code in the debugger? At what point is the execution flow not as you expect it? – Martin R Mar 05 '16 at 18:24
  • Where are you getting `category` and `label` from? It may be an issue when setting those values that selecting subtraction actually sets the value to division or something. Additionally, have a look at `else if` [syntax in swift](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement) – shamsup Mar 05 '16 at 18:33
  • 1
    Don't change the entire content of your question, it invalidates the existing answers. If you have a new, different question, create a new question. This one shoud stay on its existing topic. Thank you. – Eric Aya Mar 05 '16 at 19:49

3 Answers3

2

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.

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
1

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.

0x416e746f6e
  • 9,872
  • 5
  • 40
  • 68
1

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"
the_critic
  • 12,720
  • 19
  • 67
  • 115