twostraws / Unwrap

Learn Swift interactively on your iPhone.
https://www.hackingwithswift.com
Other
2.3k stars 312 forks source link

FreeCoding: All items capitalized. A working solution is not accepted. #214

Closed duianto closed 3 years ago

duianto commented 3 years ago

FreeCoding: All items capitalized. A working solution not accepted.

In the FreeCoding exercise:

Write code that creates an array of all the items in words, capitalized. So, your finished array should contain THE, RAIN, IN, SPAIN."

https://github.com/twostraws/Unwrap/blob/7344eeb6bef109d2710dd12dcadefc8f4fd9b236/Unwrap/Activities/Practice/FreeCoding/FreeCoding.json#L222

The following working solution isn't accepted:

let words = ["The", "rain", "in", "Spain"]
var uppercased: [String] = []
for word in words {
  uppercased.append(word.uppercased())
}

It does work on replit: https://replit.com/languages/swift

Printing the capitalized array:

print(uppercased)

Outputs:

["THE", "RAIN", "IN", "SPAIN"]

A possible cause:

The accepted answers: https://github.com/twostraws/Unwrap/blob/7344eeb6bef109d2710dd12dcadefc8f4fd9b236/Unwrap/Activities/Practice/FreeCoding/FreeCoding.json#L226

Only accepts empty arrays that are initialized like this:

var uppercased1:[String] = [String]()

The lesson: Creating empty collections, demonstrates this method: https://github.com/twostraws/Unwrap/blob/3a5ee7f0ec6315791f3f9175438c385a623ba2b4/Unwrap/Content/SixtySeconds/creating-empty-collections.html#L21

var results = [Int]()

However

Another FreeCoding question does accept:

var newArray:[Int] = []

https://github.com/twostraws/Unwrap/blob/7344eeb6bef109d2710dd12dcadefc8f4fd9b236/Unwrap/Activities/Practice/FreeCoding/FreeCoding.json#L144

According to the answers in this thread: https://stackoverflow.com/questions/30430550/how-to-create-an-empty-array-in-swift

There are multiple ways of creating an empty array.

https://stackoverflow.com/a/42464309 This commenter said: "shorthand syntax way is always preferred."

Method 1: Shorthand Syntax

var arr = [Int]()

Method 2: Array Initializer

var arr = Array<Int>()

Method 3: Array with an Array Literal

var arr:[Int] = []

Method 4: Credit goes to @BallpointBen

var arr:Array<Int> = []

And a later comment suggests two other methods (the first and last): https://stackoverflow.com/a/51258735

var myArray: Array<String> = Array()
var myArray = [String]()
var myArray: [String] = []
var myArray = Array<String>()
var myArray:Array<String> = []

But the last comment: https://stackoverflow.com/a/56917862 links to the Apple developer page (Array).

Where the Overview section https://developer.apple.com/documentation/swift/array#overview says:

You can create an empty array by specifying the Element type of your array in the declaration. For example:


// Shortened forms are preferred
var emptyDoubles: [Double] = []

// The full type name is also allowed var emptyFloats: Array = Array()


### Summary
It might be overkill to support all methods mentioned above.

But since apple suggests:
```swift
var emptyDoubles: [Double] = []

And Unwrap: Swift already accepts it in one of the FreeCoding questions, then it might be good to also accept this method in the question:

Write code that creates an array of all the items in words, capitalized. So, your finished array should contain THE, RAIN, IN, SPAIN."

https://github.com/twostraws/Unwrap/blob/7344eeb6bef109d2710dd12dcadefc8f4fd9b236/Unwrap/Activities/Practice/FreeCoding/FreeCoding.json#L222

Environment

Carrione commented 3 years ago

The issue can be closed.