UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Project 8 Challenge 3

Forums > Swift

@AE542  

Hey guys I'm really stumped on this one and I just can't think about how to solve the final challenge. I added an else statement to submit tapped function to show an alert when you input the incorrect answers. I also added an score -=1 to reduce the score with each incorrect input but the last part is racking my brains...I just can't seem to get the loadLevel() to run and continue with the current user's score onto the the next level. I know the remainder operator divides the answer into the remaining questions left but I'm really confused about what to do next honestly...I tried making a variable to handle the final score that the user had before the level loaded but to no avail....any help would really be appreciated even criticism on how to write this better.

Thanks

@objc func submitTapped(_ sender: UIButton){ guard let answerText = currentAnswer.text else { return }

        if let solutionPosition = solutions.firstIndex(of: answerText) {
            activatedButtons.removeAll()

            var splitAnswers = answersLabel.text?.components(separatedBy: "\n")
            splitAnswers?[solutionPosition] = answerText
            answersLabel.text = splitAnswers?.joined(separator: "\n")

            currentAnswer.text = ""
            score += 1// add and append

        } else {
            score -= 1
            let ac = UIAlertController(title: "Whoops!", message: "Try Again!", preferredStyle: .alert)
                            ac.addAction(UIAlertAction(title:"Cancel", style: .cancel))
                                present(ac, animated: true)

            if score % 7 == 0 {

                let ac = UIAlertController(title: "Well Done!", message: "Are you ready the next level?", preferredStyle: .alert)
                ac.addAction(UIAlertAction(title: "Let's go!",style: .default, handler: levelUp))
                present(ac, animated: true)

        }
    }

3      

I was stumped on this for awhile too and i'm not even sure if my solution is the best solution but it works. I created a variable called 'realScore' underneath where we first created the 'score' variable and used that in order to load the next level with the existing modulo code.

@objc func submitTapped(_ sender: UIButton) {

    guard let answerText = currentAnswer.text else { return }

    if let solutionPosition = solutions.firstIndex(of: answerText) {
        activatedButtons.removeAll()

        var splitAnswers = answersLabel.text?.components(separatedBy: "\n")
        splitAnswers?[solutionPosition] = answerText
        answersLabel.text = splitAnswers?.joined(separator: "\n")
        currentAnswer.text = ""
        score += 1
        realScore += 1

        if realScore % 7 == 0 {
            let ac = UIAlertController(title: "Well Done", message: "Are you ready for the next level?!", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "Let's GET IT BABY!", style: .default, handler: levelUp))
            present(ac, animated: true)
        }
    } else {
        let ac = UIAlertController(title: nil, message: "That's not quite right", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "Try again", style: .default))
        present(ac, animated: true)
        score -= 1

    }
}

The users score will persist onto the next level because 'realScore' isnt attached to any label.

4      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.