Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

maxArray Function problem! ASAP!!

I need to solve this problem! can anyone help?

Use for-in statement to complete the maxArray function that receives integer array and returns the maximum number.

func maxArray (numbers:[Int]) -> Int {

}

let arrayToMax = [43, 56, 12, 23, 87, 27, 98]
let maxNum = maxArray(arrayToMax)

2 Answers

Here you go:

func maxArray (numbers:[Int]) -> Int {
    var max = 0

    for n in numbers {
        if n > max {
            max = n
        }
    }
    return max
}

let arrayToMax = [43, 56, 12, 23, 87, 27, 98]
let maxNum = maxArray(arrayToMax)

can I skip stating

let arrayToMax = [43, 56, 12, 23, 87, 27, 98] let maxNum = maxArray(arrayToMax)

these codes?

There I fixed my answer.

No you need those for the function to work. Meaning it needs to evaluate from somewhere, in this case is using the array.

            <p>struct Queue {
    var queueArray:[String] = []
    mutating func enqueue (element:String) {
        (-----------------------------)
    }
    mutating func dequeue() -> String {
        (-------------------------------)
        (-------------------------------)
        return returnValue
    }


var newQueue = Queue()
newQueue.enqueue("Leonard")
newQueue.enqueue("Sheldon")

newQueue.enqueue("Penny")
newQueue.enqueue("Howard")
newQueue.enqueue("Rajesh")
let firstOne = newQueue.dequeue()</p>
            ```


I don't know what should go on the blank line to make codes work

can you answer my other questions that I posted ?? Thank you very much