# Book - Silver challenge (Arrays)

hi,

regarding the silver challenge for Arrays - use a For loop to reverse the order of the elements in an array. How would i approach this ?? take them off the end and stick them on the front???

i’m a bit confused about where to start here.

thanks for any hints !

I was kind of confused on that one too, here is the loop:

for item in toDolist.reverse(){
print(item)
}

and simplified:

toDolist = toDolist.reverse()

simple as that!! thank you!!

This was my long form solution using a for loop.

``````var toDoList = ["Take out the garbage", "Pay bills", "Cross off finished items"]
var reverseArray = [String]()
for item in toDoList {
reverseArray.insert(item, atIndex: 0)
}
print(reverseArray) // ["Cross off finished items", "Pay bills", "Take out the garbage"]``````

Simpler solution was identical to yours.

```toDoList = toDoList.reverse() print(toDoList) ```

[quote=“badbluberries”]This was my long form solution using a for loop.
[/quote]

Wow this is a much easier solution than mine. Should have thought of that!

``````import Cocoa

var toDoList = ["Take out garbage", "Pay bills", "Cross off finished items"] // Test the reversal by adding more elements here
var easierWay: Array<String> = toDoList.reverse() // The automated reversal

var elementCount = toDoList.count // Get the amount of elements in the array
elementCount -= 1 // Decrease one because arrays start at 0
var currentPosition = 0
var reverseElement = "" // The var where we will store the overwritten element temporarily
var loopAmount = elementCount/2 // Amount of loops necessary to reverse an array

print("Before: \(toDoList)")

for var i = loopAmount; i > 0; i-- {
reverseElement = toDoList[currentPosition] // Store the current element
toDoList[currentPosition] = toDoList[elementCount] // Overwrite the current element
toDoList[elementCount] = reverseElement // Restore the overwritten element at the mirrored index
elementCount--
currentPosition++
}

print("After: \(toDoList)") // This is reversed by our loop
print("Control array: \(easierWay)") // This is the automated reversal``````

Just where in the documentation is reverse documented? Google search finds it on other forums but I draw a blank with the Apple documentation.

reverse() is a method on CollectionType. Docs are here: developer.apple.com/library/tvo … index.html

Got it, thanks…

Just thought I would share on the first part of the challenge. I was playing around and happened to stumble my way into this solution using only the functions/methods shown in this and previous chapters. Basically copying an item to the end of the array and removing the item that was just copied.

[code]for var i = toDoList.count-1 ; i >= 0 ; i-- {
toDoList.append(toDoList[i])
toDoList.removeAtIndex(i)

}
print(toDoList)[/code]

Here is the simplest solution I could find using for loop:

``````for i in 0...(toDoList.count-1) {
toDoList.insert(toDoList.popLast()!, atIndex: i)
}``````

I managed to get SortInPlace to work (eventually):

``````toDoList.sortInPlace { \$0.capitalizedString > \$1.capitalizedString }
``````

But reverse() is far easier!

Actually, you can end the for loop one iteration earlier. In the above, the final iteration pops off the last string in the array and then puts that same string back into the last position. Hence, the last iteration does not actually result in a change to the array.

Thus, you can use:
``` for i in (0…toDoList.count-2) { toDoList.insert(toDoList.popLast()!, atIndex: i) } ```