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

Checkpoint 6 Review

Forums > 100 Days of SwiftUI

Hello world,

Below is a solution to checkpoint 6 i've drafted. The solution works, but i'd like to know if there's anything I could correct or improve. The idea here was to randomize the current gear between the two constraints listed; one and ten, as noted in the challenge. Then have the gears change respective of the amount of gears given. In this example from my testing, I get 4 (randomized), then 8, then 4 again.

Thanks.

struct Automobile {
    let model: String
    let seats: Int
    private var currentGear: Int

    init(model: String, seats: Int) {
        self.model = model
        self.seats = seats
        self.currentGear = Int.random(in: 1...10)
        print("Current gear is \(currentGear)")
    }

    mutating func gearUp(gears: Int) {
        if currentGear <= gears {
            currentGear += gears
            print("Current gear after shifting up is \(currentGear)")
        }
    }

    mutating func gearDown(gears: Int) {
        if currentGear >= gears {
            currentGear -= gears
            print("Current gear after shifting down is \(currentGear)")
        }
    }
}

var car = Automobile(model: "BMW", seats: 4)
car.gearUp(gears: 4)
car.gearDown(gears: 4)

2      

Your code works well, however you can add some errors with explanations when shifting gears fails. for example if the current gear is 5 and the user requests to add 6 to it you could print that the gears would be out of bounds if the user did that. same with gearDown(). Then you can check the model string and if it is equal to something specific(bmw for example) then the max gear numer would be X else if its something else(audi for example) then the max gear would be Y, and if those 2(or however many you might implement) return false then have a default number of gears. Lastly you should try yourself to make some challenges for yourself, creativity plays a BIG role in coding, think of it as art.

if you're stuck on any of these reply to me and i'll send you the solution(or a hint to whatever problem you may be facing). -Alex

2      

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.