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
Joyce Ryu
1,124 PointsmaxArray 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
Jhoan Arango
14,575 PointsHere 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)
Joyce Ryu
1,124 Pointscan you answer my other questions that I posted ?? Thank you very much
Joyce Ryu
1,124 PointsJoyce Ryu
1,124 Pointscan I skip stating
let arrayToMax = [43, 56, 12, 23, 87, 27, 98] let maxNum = maxArray(arrayToMax)
these codes?
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsThere 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.
Joyce Ryu
1,124 PointsJoyce Ryu
1,124 Points