TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Checkpoint 9

Forums > 100 Days of SwiftUI

Hey fellow HWSters, Here is my completed code for Checkpoint 9. I did it in a fairly simple way, so if you have any sugestions on how to improve it please let me know.

let arrayOfIntegers: [Int]? = nil

let randomInteger = arrayOfIntegers?.randomElement() ?? .random(in: 1...100)
print(randomInteger)

2      

Matthew is looking for feedback on Challenge 9.

I did it in a fairly simple way, so if you have any sugestions on how to improve it please let me know.

// You're setting a constant. This will never hold anything but nil.
let arrayOfIntegers: [Int]? = nil  

// This line of code will never execute the first part of the clause.
// This is because arrayOfIntegers will always be nil.
let randomInteger = arrayOfIntegers?.randomElement() ?? .random(in: 1...100)

The challenge was to write a function. I am not sure you met the requirements of this challenge. You didn't write a function, and your arrayOfIntegers is a constant that won't be anything but nil.

Try again?

2      

Hey @Obelix, Thank you for the feedback and here is my updated Checkpoint 9. Cheers :)

var arrayOfIntegers: [Int]? = nil

func randomInteger(arrayOfIntegers: [Int]?) -> Int {
    return arrayOfIntegers?.randomElement() ?? .random(in: 1...100)
}

print(randomInteger(arrayOfIntegers: [1, 2, 7, 9]))

2      

On his second try Matthew codes:

// var arrayOfIntegers: [Int]? = nil  // Good use case! But you never test this.
// rename to:
var nilArrayOfIntegers: [Int]? = nil // Pass this to your function as a test case!

func randomInteger(arrayOfIntegers: [Int]?) -> Int {
    return arrayOfIntegers?.randomElement() ?? .random(in: 1...100)
}

print(randomInteger(arrayOfIntegers: [1, 2, 7, 9]))

// Add this test case to get full points!
print(randomInteger(arrayOfIntegers: nilArrayOfIntegers) // Pass in the nil array from above.

Nicely done!

Keep coding!

2      

Thanks, @Obelix for the suggestions! Here is my updated code.

var nilArrayOfIntegers: [Int]? = nil

func randomInteger(arrayOfIntegers: [Int]?) -> Int {
    return arrayOfIntegers?.randomElement() ?? .random(in: 1...100)
}

print(randomInteger(arrayOfIntegers: [1, 2, 7, 9]))
print(randomInteger(arrayOfIntegers: nilArrayOfIntegers))

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your 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.