WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.