|
In Checkpoint 9, Paul challenges us to write a function that accepts an optional array of integers and returns a random value from the array. However, if the function's parameter is nil, Paul wants us to use a default array of values from 1 through 100. The twist? Do this in one line of code. First I thought of how would you name such a function. I toyed with a few names and settled on "pickOne". The parameter has two names, one for external use, the other for internal use. What function name did you pick? Add your comments below and share your thoughts.
This allows a user to write:
Next, regardless of what is provided, return a random value between 1 and 100.
The odd thing here is even though I'm providing a valid integer array, the randomElement() method returns an optional. So I need to force unwrap it. I'm not afraid of this crashing, because indeed I am providing a bona fide array of Ints. Next, figure out if the user provided a valid array, or nil.
This is an optional chain. And either of these can return nil. First, the array might not even exist, thus returning nil. Second, if the array exists, it might be an empty array, so randomElement() will return nil as well. In this case, the nil coalescing operator comes to the rescue. If the first part returns nil, then provide a value from the second part of the nil coalescing operation. Also, because there is only one line that creates an Int value, Swift considers this the return value. No need to use the return keyword.
I think this qualifies as a one line function. |
|
Because what should it return if you call |
|
This is what I came up with
|
|
@Feras-Eng Elegant solution. Why should I create an array, then pick a random element from it. (Plus the overhead of unwrapping it!) Your solution of just picking a random number in a closed range is brilliant! Well done. I know these checkpoints are supposed to help us understand concepts, but as your example shows, it also helps us think through many ways to accomplish a task. Your solution is much cleaner. Likewise, I offer this observation to you. Your function's goal is to select a random number from within an array. Does your method's name and parameter make this clear?
|
|
hello every one i'm new here i have question to @Obelix about his soluton for the Checkpoint 9 is it necessarily to do the "!" after Array(1...100).randomElement() |
|
Rami asks an important question!
This is a great observation. And this is an important lesson about Optionals. The answer is "YES" you must add the "!" after the code See @rooster's answer above. But here's a bit more explanation: The reason is important for you to understand. It has to do with Optionals. You and I both see that I'm creating an array with numbers from 1 to 100, we can conclude this array IS NOT EMPTY. Therefore, it is safe to assume that I can ask Swift to select a random element from the array and it will return one of the elements. However, the By definition, the At this point in your learning career, you'll want to start to get familiar with Apple's Swift Documentation. Get comfortable finding documentation about methods, reading the requirements, and learing about the return types. Here's the documentation for Array's See -> randomElement() |
|
Hello team, This is my solution, I had to go back and review closures. This gives me a function declaration in one line of code. Cheers.
|
|
I know it's years after the battle but here is the code I came up with. It seems to work and it's on one line
It's so simple I'm sure I missed something. Do you have anything that I could improve on this piece of code ? |
SAVE 50% All our books and bundles are half price for Black Friday, 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.
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.
Link copied to your pasteboard.